-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue using Stack.cpp
executable file
·82 lines (71 loc) · 1.17 KB
/
Queue using Stack.cpp
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
//Queue using stack
#include<stdio.h>
#define N 5
int s1[N],s2[N]; //Define 2 stack
int count=0,top_1=-1,top_2=-1; //count for tracking total element in queue
void push_1(int x){
if(top_1==(N-1)){
printf("Queue is full\n");
count--;
}
else{
top_1++;
s1[top_1]=x;
}
}
int pop_1(){
int a=s1[top_1];
top_1--;
return a;
}
void push_2(int x){
if(top_2==(N-1)){
printf("Queue is full\n");
}
else{
top_2++;
s2[top_2]=x;
}
}
int pop_2(){
int a=s2[top_2];
top_2--;
return a;
}
void enqueue(int x){
push_1(x);
count++;
}
void dequeue(){
for(int i=0;i<count;i++){
push_2(pop_1());
}
int a=pop_2();
printf("Removed :%d\n",a);
count--;
for(int i=0;i<count;i++){
push_1(pop_2());
}
}
void display(){
int a=top_1;
for(int i=0;i<=a;i++){
printf("%d ",s1[i]);
}
printf("\n");
}
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
enqueue(6);
display();
dequeue();
display();
enqueue(10);
display();
dequeue();
display();
}