-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwo_Stacks_With_Single_Array.cpp
140 lines (118 loc) · 3.84 KB
/
Two_Stacks_With_Single_Array.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
#include <iostream>
using namespace std;
#define SIZE 20
/*---------------------------------------------------------------------------*/
int stack[SIZE];
int top1 = -1;
int top2 = SIZE;
/*---------------------------------------------------------------------------*/
//Function to push data into stack1
void push1 (int data){
if (top1 < top2 - 1){ // Checking the overflow condition
top1++;
stack[top1] = data;
cout << data << " - Pushed successfully to Stack 1\n";
}
else {
cout << "Stack is full";
}
}
/*---------------------------------------------------------------------------*/
// Function to push data into stack2
void push2 (int data){
if (top1 < top2 - 1){ // checking overflow condition
top2--;
stack[top2] = data;
cout << data << " - Pushed successfully to Stack 2\n";
}
else{
cout << "Stack is full\n";
}
}
/*---------------------------------------------------------------------------*/
//Function to pop data from the Stack1
void pop1 (){
if (top1 >= 0){ // Checking the underflow condition
int popped_element = stack[top1];
top1--;
cout << popped_element << " - Popped successfully from Stack 1\n";
}
else{
cout << "Stack is Empty\n";
}
}
/*---------------------------------------------------------------------------*/
// Function to remove the element from the Stack2
void pop2 (){
if (top2 < SIZE){ // Checking underflow condition
int popped_element = stack[top2];
top2++;
cout << popped_element << " - Popped successfully from Stack 2\n";
}
else{
cout << "Stack is Empty\n";
}
}
/*---------------------------------------------------------------------------*/
//Functions to Print the values of Stack1
void display_stack1(){
int i;
for (i = top1; i >= 0; --i){
cout << stack[i];
}
cout << endl;
}
/*---------------------------------------------------------------------------*/
// Function to print the values of Stack2
void display_stack2(){
int i;
for (i = top2; i < SIZE; ++i){
cout << stack[i];
}
cout << endl;
}
/*---------------------------------------------------------------------------*/
// Main Function
int main(){
int code;
char ch;
cout << "Enter \n1 ————> push (Stack 1)\n2 ————> push (Stack 2)\n3 ————> pop (Stack 1)\n4 ————> pop (Stack 2)\n5 ————> display (Stack 1)\n6 ————> display (Stack 2)\n\n";
do{
int num;
cout << "\nEnter a number corresponding to your choice : ";
cin >> num;
switch(num){
case 1:
cout << "Enter a Number : ";
cin >> code;
push1(code);
break;
case 2:
cout << "Enter a Number : ";
cin >> code;
push2(code);
break;
case 3:
pop1();
break;
case 4:
pop2();
break;
case 5:
cout << endl;
display_stack1();
break;
case 6:
cout << endl;
display_stack2();
break;
default :
cout << "Invalid Choice\n";
}
cout << "\nDo you want to continue? (Y / N) : ";
cin >> ch;
cout << endl;
}while(ch == 'Y' || ch == 'y');
return 0;
}
/*---------------------------------------------------------------------------*/