-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack using queue.cpp
More file actions
95 lines (94 loc) · 1.43 KB
/
Stack using queue.cpp
File metadata and controls
95 lines (94 loc) · 1.43 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
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<conio.h>
#include<iostream>
#define sz 5
class queue
{
private:int s[sz],f=-1,r=-1;
public: int insert(int x)
{
if(r>=sz-1)
return 0;
r++;
s[r]=x;
if(f==-1)
f++;
return 1;
}
int del()
{
int i;
if(f==-1)
return 0;
i=s[f];
if(++f>r)
{
f=-1;
r=-1;
}
return i;
}
void display()
{
int i;
printf("\n");
for(i=f;f!=-1&&i<=r;i++)
printf("%d ",s[i]);
}
};
class stack
{
private:queue q1,q2;
public: void push(int x)
{
if(!x)
printf("\n%d is not inserted!!!",x);
if(!q1.insert(x))
printf("\nstack overflow!!!!");
}
void pop()
{
int x,y;
x=q1.del();
y=q1.del();
for(;y;)
{
q2.insert(x);
x=y;
y=q1.del();
}
if(x)
printf("\n%d deleted\n",x);
else
printf("\nqueue under flow\n");
x=q2.del();
for(;x;)
{
q1.insert(x);
x=q2.del();
}
}
void display()
{
q1.display();
}
};
int main()
{
stack a;
int z;
printf("menu\n 1.insert\n 2.delete\n 3.display\n 0.exit\nenter choice...");
for(;1;)
{ switch(getche())
{ case '1': printf("\n enter element to insert..");
scanf("%d",&z);
a.push(z);
break;
case '2': a.pop(); break;
case '3': a.display(); break;
case '0': return 0; break;
default : printf("\n wrong choice !!!!");
}
printf("\nenter choice...");
}
}