-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.c
More file actions
86 lines (73 loc) · 2.03 KB
/
Copy path22.c
File metadata and controls
86 lines (73 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
// Define the maximum size of the stacks
#define MAX 8
// Define the structure for the stack
struct Stack {
int arr[MAX];
int top;
};
// Initialize the stacks
void initStack(struct Stack *s) {
s->top = -1;
}
// Check if a stack is empty
int isEmpty(struct Stack *s) {
return (s->top == -1);
}
// Check if a stack is full
int isFull(struct Stack *s) {
return (s->top == MAX - 1);
}
// Push an element onto a stack
void push(struct Stack *s, int value) {
if (isFull(s)) {
printf("Stack is full\n");
return;
}
s->top++; // top is incremented or top = top+1;
s->arr[s->top] = value;//store the value in the array of stack
}
// Pop an element from a stack
int pop(struct Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty\n");
return -1;
}
int value = s->arr[s->top]; // What ever value deleted stored in value, after poped out
s->top--; //top is going to be decreased or top = top - 1;
return value;
}
// Enqueue an element into the queue
void enqueue(struct Stack *s1, struct Stack *s2, int value) {
// Push all elements from s1 to s2
while (!isEmpty(s1)) { // that means while s1 stack is not empty
push(s2, pop(s1)); // it should push s2 and pop s1
}
// Push the new element onto s1
push(s1, value);
// Push back all elements from s2 to s1
while (!isEmpty(s2)) {
push(s1, pop(s2));
}
}
// Dequeue an element from the queue
int dequeue(struct Stack *s1) {
if (isEmpty(s1)) {
printf("Queue is empty\n");
return -1;
}
return pop(s1);
}
int main() {
struct Stack stack1, stack2;
initStack(&stack1);
initStack(&stack2);
enqueue(&stack1, &stack2, 80);
enqueue(&stack1, &stack2, 90);
enqueue(&stack1, &stack2, 30);
printf("Dequeued: %d\n", dequeue(&stack1));
printf("Dequeued: %d\n", dequeue(&stack1));
printf("Dequeued: %d\n", dequeue(&stack1));
return 0;
}