-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQueue using Two Stacks
126 lines (109 loc) · 1.7 KB
/
Queue using Two Stacks
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int MAX=100000;
int q[100000],stack2[100000],stack[100000];
int front=-1,rear=-1,size=0;
int top=-1;
int top2=-1;
void push(int x)
{
if(top==MAX-1)
printf("overflow \n");
else
{
stack[++top]=x;
}
}
int pop()
{
int x;
if(top==-1){
printf("underflow \n");
return -1;
}
else{
x=stack[top--];
stack2[++top2]=x;
}
return x;
}
int pop2()
{
int x;
if(top2==-1){
printf("underflow \n");
return -1;
}
else
x=stack2[top2--];
return x;
}
int enqueue(int item)
{
rear=(rear+1);
if(front==rear)
{
printf("Queue is full");
return -1;
}
else
{
push(item);
}
return -1;
}
int dequeue()
{
int item;
if(front==rear)
{
printf("Aueue is empty");
return -1;
}
if(top2 == -1)
{
while(top!= -1)
{
item = pop();
}
item = pop2();
}
else
item= pop2();
return item;
}
void print(){
int x;
if(top2!=-1){}else{
while(top!=-1)
{
pop();
}
}
x=stack2[top2];
printf("%d\n",x);
}
int main()
{
int x,value,T,t=0;
scanf("%d",&T);
do{
scanf("%d",&x);
t++;
switch(x){
case 1:
//printf("enter value to be pushed");
scanf("%d",&value);enqueue(value);
break;
case 2:dequeue();
break;
case 3:print();
case 4:break;
default: printf("Invalid case");
break;
}
}while(T!=t);
return 0;
}