-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue_Using_Stack.cpp
144 lines (120 loc) · 3.79 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
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
#include <iostream>
using namespace std;
#define N 10
/*---------------------------------------------------------------------------*/
int stack1[10], stack2[10]; // declaration of two stacks
int top1 = -1, top2 = -1; // declaration of top variables.
int count = 0;
/*---------------------------------------------------------------------------*/
// inserting the elements in stack1.
void push1(int data){
// Condition to check whether the stack1 is full.
if(top1 == N - 1){
cout << "\nStack Overflow";
}
else {
top1++; // Incrementing the value of top1
stack1[top1] = data; // pushing the data into stack1
}
}
/*---------------------------------------------------------------------------*/
// Removing the elements from the stack1.
int pop1(){
// Condition to check whether the stack1 is empty.
if(top1 == -1){
cout << "\nStack is Empty";
}
else{
int a = stack1[top1]; // Assigning the topmost value of stack1 to 'a' variable.
top1--; // decrementing the value of top1.
return a;
}
}
/*---------------------------------------------------------------------------*/
// pushing the data into the stack2.
void push2(int x){
// Condition to check whether the stack2 is full.
if(top2 == N - 1){
cout << "\nStack is full";
}
else{
top2++; // incrementing the value of top2.
stack2[top2] = x; // assigning the 'x' value to the Stack2
}
}
/*---------------------------------------------------------------------------*/
// Removing the elements from the Stack2
int pop2(){
// Condition to check whether the stack2 is empty.
if(top2 == -1){
cout << "\nStack is Empty";
}
else{
int element = stack2[top2]; // assigning the topmost value to element
top2--; // decrement the value of top2
return element;
}
}
/*---------------------------------------------------------------------------*/
void enqueue(int x){
push1(x);
count++;
}
/*---------------------------------------------------------------------------*/
void dequeue(){
if((top1 == -1) && (top2 == -1)){
cout << "\nQueue is empty";
}
else{
for(int i = 0; i < count; i++){
int element = pop1();
push2(element);
}
int b = pop2();
cout << "\nThe dequeued element is " << b;
cout << endl;
count--;
for(int i = 0; i < count; i++){
int a = pop2();
push1(a);
}
}
}
/*---------------------------------------------------------------------------*/
void display(){
for(int i = 0; i <= top1; i++){
cout << stack1[i] << " ";
}
cout << endl;
}
/*---------------------------------------------------------------------------*/
int main(){
int ch, num;
cout << "1 —————> Insert\n";
cout << "2 —————> Delete\n";
cout << "3 —————> Display\n";
cout << "4 —————> Exit\n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the number to be inserted : ";
cin >> num;
enqueue(num);
break;
case (2):
dequeue();
break;
case (3):
display();
break;
case (4):
break;
default:
cout << "Enter a number between 1 and 4";
}
}while(ch != 4);
return 0;
}
/*---------------------------------------------------------------------------*/