-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
102 lines (98 loc) · 2 KB
/
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
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX_SIZE 10
int stack1[MAX_SIZE];
int stack2[MAX_SIZE];
int top1 = -1;
int top2 = -1;
bool isFull(int top)
{
if (top1 == MAX_SIZE - 1)
{
return true;
}
return false;
}
bool isEmpty(int top)
{
if (top == -1)
{
return true;
}
return false;
}
void push(int *stack, int top, int val)
{
if (isFull(top))
{
cout << "cant add" << endl;
return;
}
stack[++top] = val;
}
void pop(int *stack, int top)
{
if (isEmpty(top))
{
cout << "cant pop " << endl;
return;
}
--top;
}
void display(int *stack, int top)
{
if (isEmpty(top))
{
cout << "STACK IS empty" << endl;
return;
}
for (int i = 0; i <= top; i++)
{
cout << stack[i] << endl;
}
}
int main()
{
int choice;
do
{
cout << "Choose an operation:\n";
cout << "1. Push stack1 \n2. Pop stack1 \n3. Display stack1\n";
cout << "4. Push stack2 \n5. Pop stack2 \n6. Display stack2\n7. Exit\n";
cin >> choice;
switch (choice)
{
case 1:
int val1;
cout << "Enter a value to push: ";
cin >> val1;
push(stack1, top1, val1);
break;
case 2:
pop(stack1, top1);
break;
case 3:
display(stack1, top1);
break;
case 4:
int val2;
cout << "Enter a value to push: ";
cin >> val2;
push(stack2, top2, val2);
break;
case 5:
pop(stack2, top2);
break;
case 6:
display(stack2, top2);
break;
case 7:
cout << "Exiting...\n";
break;
default:
cout << "Invalid choice. Try again.\n";
}
} while (choice != 4);
return 0;
}