-
Notifications
You must be signed in to change notification settings - Fork 0
/
dystack.c
72 lines (69 loc) · 1.27 KB
/
dystack.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
#include<stdio.h>
#include<stdlib.h>
#define MALLOC(p,s) \
if(!(p=malloc(s))){\
fprintf(stderr,"Insufficient memory \n");\
exit(0); \
}
#define REALLOC(p,n,type)\
if(!(p = (type *)realloc(p,n))) { \
printf("Insufficient memory \n");\
exit(0);\
}
int ssize = 1;
void push(char *,int,int);
char pop(char *,int,int);
void main()
{
int choice,done=0,top=-1;
char *a,pe,e;
MALLOC(a,sizeof(int));
while(!done)
{
printf("\n Stack operations: \n 1.Push \n 2.Pop \n 3.Display 4.Exit \n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter elements \n");
scanf("%d",&e);
push(*a,&top,e);
break;
case 2: pe = pop(*a,&top);
if(pe == -1)
printf("Stack Underflow \n");
else
printf("Popped element is %d \n",pc);
break;
case 3: if(top == -1)
printf("Empty stack \n");
else
{
for(i=top;i>=0;i--)
printf("%d \n",*(a+i));
}
break;
case 4: done = 1;
break;
default: printf("Enter a valid number \n");
break;
}
}
}
char pop(char *s,int *top)
{
(*top)--;
return *(s+*top);
}
void push(char *sa,int *top,int s)
{
if(*top == (ssize-1))
{
printf("Stack is full \n");
printf("Increasing stack size \n");
ssize++;
REALLOC(*s,ssize,int);
}
*top = *top++;
(*s)[*top]=s;
}