-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-using-array.cpp
More file actions
124 lines (122 loc) · 2 KB
/
Copy pathstack-using-array.cpp
File metadata and controls
124 lines (122 loc) · 2 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
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
#include <iostream>
using namespace std;
class stack
{
private:
int capacity;
int top;
int *ptr;
public:
stack(int cap)
{
capacity =cap;
top=-1;
ptr= new int[cap];
}
~stack()
{
delete []ptr;
}
int is_full()
{
if(top==capacity-1)
return(1);
else
return(0);
}
int is_empty()
{
if(top==-1)
return(1);
else return(0);
}
void push(int val)
{
if(is_full())
cout<<"Stack Overflow"<<endl;
else
{
top++;
ptr[top]=val;
}
}
void pop()
{
if(is_empty())
cout<<"Stack Underflow"<<endl;
else
{
int i;
i=ptr[top];
top--;
cout<<i<<endl;
}
}
void peep()
{
if(is_empty())
cout<<"Stack Underflow"<<endl;
else
cout<<ptr[top]<<endl;
}
void display()
{
if(is_empty())
cout<<"Stack Underflow"<<endl;
else
{
int i=top;
while(i!=-1)
{
cout<<ptr[i]<<endl;
i--;
}
}
}
void countlements()
{
cout<<"Number of elemets in the stack are "<<top+1<<endl;
}
};
int menu()
{
int choice;
cout<<"1. Push value\n2. Pop Value\n3. Peep value\n4. Count Elements of array\n5. Display Array\n6. Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
return(choice);
}
int main()
{
int c;
cout << "Enter the capacity of stack"<<endl;
cin>>c;
stack s(c);
int value;
while(1)
switch(menu())
{
case 1:
cout<<"Enter a value"<<endl;
cin>>value;
s.push(value);
break;
case 2:
s.pop();
break;
case 3:
s.peep();
break;
case 6:
cout<<"Thankyou"<<endl;
exit(0);
case 4:
s.countlements();
break;
case 5:
s.display();
break;
default:
cout<<"Invalid Choice"<<endl;
}
}