-
Notifications
You must be signed in to change notification settings - Fork 2
/
stack_using_ar.c
84 lines (73 loc) · 938 Bytes
/
stack_using_ar.c
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
#include<stdio.h>
#include<stdlib.h>
struct nodes
{
int info;
struct nodes *next;
};
void push();
void pop();
void traverse();
typedef struct nodes snodes;
int nodes=0,j,k=0;
int stack[50],top=-1,n;
void main()
{
printf("Enter the size of array:");
scanf("%d",&n);
int ch;
while(1)
{
printf("Enter your choice:\n 1.Push elements into stack\n2.Pop\n3.Traverse\n4.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
default:
exit(0);
}
}
}
void push()
{
int x;
if(top>=n-1)
printf("STACK OVERFLOW");
else
{
printf("ENter a value:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
printf("STACK UNDERFLOW");
else
top--;
}
void traverse()
{
int x;
if(top<=-1)
printf("STACK EMPTY");
else
{
x=top;
while(x!=-1)
{
printf("%d",stack[x]);
x--;
}
}
}