-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.Stack_&_Queue.cpp
164 lines (138 loc) · 2.82 KB
/
2.Stack_&_Queue.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//13
#include<iostream>
#include<list>
using namespace std;
class linklist
{
private:
list<int>s1; //creating object of list of type int for stack
list<int>::iterator ptr; //IMP input iterator as used to read from containers
list<int>q;
list<int>::iterator ptr2;
public:
//----------------------------------------------------------------
void push_stack()
{
int n;
cout<<"size before : "<<s1.size()<<"\n\n";//IMP
while(1)
{
cout<<"Enter number : ";
cin>>n;
if(n == 0)
break;
s1.push_back(n);
}
cout<<"\nsize after : "<<s1.size();
}
//----------------------------------------------------------------
void display_stack()
{
ptr=s1.end(); //it points for eg. to /0
ptr--; //therefore decrement by 1
if(s1.size()==0) //IMP
{
cout<<"\nSTACK IS EMPTY!!"<<endl;
return;
}
for(;ptr!=s1.begin();ptr--)
{
cout<<"Number : "<<*ptr<<endl;
}
cout<<"Number : "<<*ptr<<endl;
}
//-------------------------------------------------------------
void pop_stack()
{
if(s1.size()==0)
{
cout<<"\nSTACK IS EMPTY!!"<<endl;
return;
}
s1.pop_back();
cout<<"\nELEMENT DELETED";
}
//****************************************************************
void insert_queue()
{
int n;
cout<<"size before : "<<q.size()<<"\n\n";
while(1)
{
cout<<"Enter number : ";
cin>>n;
if(n == 0)
break;
q.push_back(n); //
}
cout<<"\nsize after : "<<q.size();
}
//---------------------------------------------------------------
void display_queue()
{
if(q.size()==0)
{
cout<<"\nQUEUE IS EMPTY!!"<<endl;
return;
}
ptr2=q.begin();
// cout<<"1st = "<<*ptr2<<endl;
for(;ptr2!=q.end();ptr2++)
{
cout<<"Number : "<<*ptr2<<endl;
}
}
//---------------------------------------------------------------
void delete_queue()
{
if(q.size()==0)
{
cout<<"\nQUEUE IS EMPTY!!"<<endl;
return;
}
q.pop_front();
cout<<"\nELEMENT DELETED";
}
//----------------------------------------------------------------
};
main()
{
linklist l;
int op=-1;
while(op!=0)
{
cout<<"\n\n1.PUSH TO STACK";
cout<<"\n2.DISPLAY STACK";
cout<<"\n3.POP FROM STACK";
cout<<"\n4.INSERT IN QUEUE";
cout<<"\n5.DISPLAY IN QUEUE";
cout<<"\n6.DELETE FROM QUEUE";
cout<<"\n7.EXIT";
cout<<"\n\nENTER CHOICE : ";
cin>>op;
switch(op)
{
case 1:
l.push_stack();
break;
case 2:
l.display_stack();
break;
case 3:
l.pop_stack();
break;
case 4:
l.insert_queue();
break;
case 5:
l.display_queue();
break;
case 6:
l.delete_queue();
break;
case 7:
op=0;
break;
}
}
}