-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdynamic_stack.c
More file actions
82 lines (81 loc) · 1.97 KB
/
Copy pathdynamic_stack.c
File metadata and controls
82 lines (81 loc) · 1.97 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
#include<stdio.h>
#include<stdlib.h>
int isfull(int,int);
int isempty(int);
void push(int,int *,int []);
int pop(int *,int,int []);
void display(int,int []);
int main()
{
int c=1,top=-1;
int *dstk,ele;
dstk=(int*)malloc(c*sizeof(int));
int ch;
while(1)
{
printf("1.push\n2.pop\n3.display\n4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(isfull(top,c))
{
c*=2;
printf("in if c is %d\n",c);
dstk=(int*)realloc(dstk,c*sizeof(int));
}
printf("enter an element to be pushed in to the stack : ");
scanf("%d",&ele);
push(ele,&top,dstk);
break;
case 2: if(isempty(top))
{
printf("The stack is empty\n");
}
else
{
ele=pop(&top,c,dstk);
printf("The deleted element of the stack is %d\n",ele);
}
break;
case 3: if(top!=-1)
display(top,dstk);
else
printf("The stack is empty\n");
break;
case 4: exit(0);
break;
default:printf("Invalid input please try aganing \n");
}
}
return 0;
}
int isfull(int t,int cap)
{
if(t==(cap-1))
return 1;
return 0;
}
int isempty(int t)
{
if(t==-1)
return 1;
return 0;
}
void push(int e,int *t,int s[])
{
(*t)++;
s[*t]=e;
}
int pop(int *t,int cap,int *s)
{
int val=s[*t];
(*t)--;
s=(int*)realloc(s,(cap*sizeof(int))-1);
return val;
}
void display(int t,int s[])
{
for(;t>=0;t--)
printf("%d ",s[t]);
printf("\n");
}