-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue_stack.c
99 lines (76 loc) · 1.34 KB
/
queue_stack.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
//queue using stack
// Dequeue is costly
#include<stdio.h>
int arr[10];
int brr[10];
int size1 = sizeof(arr)/sizeof(arr[0]);
int size2 = sizeof(brr)/sizeof(brr[0]);
int top1 = -1;
int top2 =-1;
void push1(int val){
if(top1==(size1-1)){
printf("Overflow");
}
else{
top1 = top1+1;
arr[top1]=val;
}
}
void pop1(){
if(top1==-1){
printf("undeflow");
}
else{
top1--;
}
}
void push2(int val){
if(top2==(size2-1)){
printf("Overflow");
}
else{
top2 = top2+1;
brr[top2]=val;
}
}
void pop2(){
if(top2==-1){
printf("undeflow");
}
else{
top2--;
}
}
void enqueue(int val){
push1(val);
}
void dequeue(){
while(top1!=-1){
int ele = arr[top1];
push2(ele);
pop1();
}
pop2();
printf("\nThe element is deleted");
while(top2!=-1){
int el = brr[top2];
push1(el);
pop2();
}
}
void show(){
printf("\nThe element in queue is : \n");
for(int i=0;i<(top1+1);i++){
printf("%d\t",arr[i]);
}
}
int main(){
for(int i=1;i<=5;i++){
enqueue(i*3);
}
show();
dequeue();
dequeue();
show();
return 0;
}