-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackArray.cpp
98 lines (63 loc) · 1.08 KB
/
StackArray.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
#include <stdio.h>
#include <stdlib.h>
const int max=1000;
int a[max],top=-1;
void push(int data);
void pop();
void peep();
int main()
{
int data;
char c;
while(1)
{
printf("What do you want to do? \n a. Push \n b.Pop \n c.Peep \n d.Exit \n ");
scanf(" %c",&c);
switch(c)
{
case 'a': if(top==(max-1))
{
printf("The stack is full. No more values can be pushed. \n");
continue;
}
printf("Enter the value you want ot push onto the stack \n");
scanf("%d",&data);
push(data);
printf("Value pushed \n");
break;
case 'b': pop();
break;
case 'c': peep();
break;
case 'd': exit(0);
default : printf("Invalid option. Try again\n");
}
}
}
void push(int data)
{
a[++top]= data;
}
void pop()
{
if(top<0)
{
printf("The stack is empty\n");
return;
}
printf("The value popped is : %d \n",a[top]);
top--;
}
void peep()
{
int i;
if(top<0)
{
printf("The stack is empty\n");
return;
}
printf("\n\n");
for(i=0;i<=top;i++)
printf(" %d \n",a[i]);
printf("\n\n");
}