-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
87 lines (75 loc) · 1.4 KB
/
stack.c
File metadata and controls
87 lines (75 loc) · 1.4 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
// implementing stack using arrays
# include<stdio.h>
# include<stdlib.h>
# define max 5
int stack[max],value,n,top = -1;
// function to enter element in stack
void push()
{
if(top == max-1)
{
printf("Stack overflow \n");
}
else
{
top = top+1;
printf("Enter Value :");
scanf("%d",&value);
stack[top] = value;
}
}
// function to delete last(top) element from stack
void pop()
{
if(top == -1 )
{
printf("Stack underflow \n");
}
else
{
value = stack[top];
top = top-1;
printf("The deleted element is %d \n",value);
}
}
// function to print last(top) element without deletion
void peep()
{
if(top == -1)
{
printf("Stack empty \n");
}
else
{
value = stack[top];
printf("The top value is %d \n",value);
}
}
void main()
{
int ch,stack[max];
// display of choices
printf(" \n 1. Push \n 2. Pop \n 3. Peep \n ");
while(1)
{ // selection of choices
printf("Enter your choice : \n");
scanf("%d",&ch);
switch(ch)
{ // cases to be performed
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
exit(n);
break;
default:
printf("Enter a valid choice .... LOL \n");
}
}
}