-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
71 lines (71 loc) · 1.43 KB
/
stack.c
File metadata and controls
71 lines (71 loc) · 1.43 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
#include <stdio.h>
#define MAX 3
int top = -1, stack[MAX];
void push(int item);
int pop();
void display();
void main()
{
int choice, item,i;
do{
printf("\n Menu\n1.push\n2.pop\n3.Display all stack element\n5.quit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter the item to be pushed: ");
scanf("%d", &item);
push(item);
break;
case 2:
item = pop();
printf("\nPopped item:%d", item);
break;
case 3:
if (top == -1)
{
printf("\nStak under flow");
}
printf("Stack Elements : ");
for (i = 0; i <=top; i++)
{
printf("%d ", stack[i]);
}
printf("\n");
printf("Top position= %d",top);
break;
case 4:
break;
default:
printf("Wrong choice");
}
}while(choice!=4);
}
void push(int item)
{
if (top < MAX - 1)
{
top = top + 1;
stack[top] = item;
}
else if (top == MAX - 1)
{
printf("\nStack over flow");
return;
}
}
int pop()
{
int item;
if (top == -1)
{
printf("\nStak under flow");
}
else
{
item = stack[top];
top = top - 1;
return item;
}
}