-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathstackarray.cpp
More file actions
80 lines (80 loc) · 1.34 KB
/
stackarray.cpp
File metadata and controls
80 lines (80 loc) · 1.34 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
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout<<"Stack OverFlow.";
return;
}
stk[++top]=x;
cout <<"Inserted element is: " <<x;
}
void pop()
{
if(top <0)
{
cout <<"Stack UnderFlow.";
return;
}
cout <<"Deleted element is: " <<stk[top--];
}
void topp()
{
{
if(top <0)
{
cout <<"Stack UnderFlow.";
return;
}
cout <<"Element at the top is : " <<stk[top]<<endl;
}
}
void display()
{
if(top<0)
{
cout <<"Stack is Empty.";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
void main()
{
clrscr();
int ch;
stack st;
cout<<"*****Stack Implementation using array*****";
while(1)
{
cout<<"\n1.Push 2.Pop 3.Top 4.Display 5.Exit\nEnter your choice: "<<endl;
cin>>ch;
switch(ch)
{
case 1: cout <<"\nEnter the element to be added: "<<endl;
cin>>ch;
st.push(ch);
break;
case 2: st.pop();
break;
case 3: st.topp();
break;
case 4: st.display();
break;
case 5: exit(0);
}
}
getch();
}