-
Notifications
You must be signed in to change notification settings - Fork 48
/
Stack Array.cpp
82 lines (78 loc) · 1.25 KB
/
Stack 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
#include <stdio.h>
#include <conio.h>
#define CAPACITY 10
int stack[CAPACITY],top=-1;
void push(int);
int pop(void);
void peek(void);
void traverse(void);
void main()
{
int ch,n,item;
scanf("%d",&ch);
do
{
printf("\nStack operation");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Peek");
printf("\n4. Traverse");
printf("\n5. Exit");
scanf("%d",&n);
switch(n)
{
case 1 : printf("\n Enter the element");
scanf("%d",&item);
push(item);
break;
case 2 : item = pop();
if(item==0)
printf("\nStack Underflow");
else
printf("\nPopped Item %d",item);
break;
case 3 : peek();
break;
case 4 : traverse();
break;
default : printf("\nInvalid");
}
}while(ch==5);
}
void push(int ele)
{
if(top==CAPACITY-1)
printf("\nStack Overflow");
else
{
top++;
stack[top]=ele;
}
}
int pop()
{
if(top==-1)
return 0;
else
return stack[top--];
}
void peek()
{
if(top==-1)
printf("\nStack Empty");
else
printf("\nPresent element %d",stack[top]);
}
void traverse()
{
if(top==-1)
printf("\nStack Empty");
else
{
int i;
for(i=0;i<=top;i++)
{
printf("\nElements in stack are %d",stack[i]);
}
}
}