-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdystack1.c
66 lines (65 loc) · 1.18 KB
/
dystack1.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
#include<stdio.h>
#include<stdlib.h>
#define MALLOC(p,s)\
if(!(p=malloc(s))) { \
printf("Insufficient memory \n");\
exit(0);\
}
#define REALLOC(p,s,type) \
if(!(p=(type *)realloc(p,s))) {\
printf("Insufficient memory \n");\
exit(0); \
}
int ssize=10;
void push(char **, int *, char);
char pop(char **,int *);
void main()
{
char *s,e,pe;
int top = -1,done=0,choice,i;
MALLOC(s,ssize*sizeof(int));
while(!done)
{
printf("Stack Operations \n 1.Push \n 2.Pop \n 3.Display \n 4.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element \n");
scanf("%c",&e);
push(*s,&top,e);
break;
case 2: pe = pop(*s,&top);
if(pe == -1)
printf("NULL\n");
else
printf("%c \n",pe);
break;
case 3: if(top == -1)
printf("Underflow \n");
else {
for(i=top;i>0;i--)
printf("%d \n",*(s+i));
}
break;
case 4: done =1;
break;
}
}
}
void push(char **s,int *top, char e)
{
if(*top == (ssize-1))
{
printf("Stack is full \n");
printf("increasing stack size \n");
ssize++;
REALLOC(**s,ssize*sizeof(int),int);
}
(*top)++;
*(s+*top)= e;
}
char pop(char **s, int *top)
{
return *(s+*top);
(*top)--;
}