-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSTACK.C
More file actions
84 lines (84 loc) · 1.32 KB
/
STACK.C
File metadata and controls
84 lines (84 loc) · 1.32 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
#include<stdio.h>
#include<conio.h>
typedef struct stack
{
int data[20];
int top;
}stack;
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
void print(stack *);
void main()
{
stack s;
int x, op;
init(&s);
clrscr();
do{
printf("\n\n1)Push\n2)Pop\n3)Print\n4)Quit");
printf("\nEnter your choice");
scanf("%d",&op);
switch(op)
{
case 1 : printf("Enter a number");
scanf("%d",&x);
if(!full(&s))
push(&s,x);
else
printf("Stack is full");
break;
case 2 : if(!empty(&s))
{
x=pop(&s);
printf("Popped value=%d",x);
}
else
printf("Stack is empty");
break;
case 3 :
if(empty(&s))
printf("Stack is empty");
else
print(&s);
break;
}
}while(op!=4);
}
void init(stack *s)
{
s->top=-1 ;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
return(0);
}
int full(stack *s)
{
if(s->top==19)
return(1);
return(0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
void print(stack *s)
{
int i;
printf("\n");
for(i=s->top;i>=0;i--)
printf("%d",s->data[i]);
}