-
Notifications
You must be signed in to change notification settings - Fork 64
/
dynamic stack.c
91 lines (79 loc) · 1.31 KB
/
dynamic stack.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
84
85
86
87
88
89
90
91
#include<stdio.h>
#include<stdlib.h>
typedef struct st
{
int data;
struct st *next;
}stack;
stack *top;
//push
void push(int x)
{
stack *temp,*p;
temp=(stack *)malloc(sizeof(stack));
temp->data=x;
temp->next=NULL;
if(top==NULL)
top=temp;
else
temp->next=top;
top=temp;
}
//pop
void pop()
{
int x;
stack *p;
p=top;
top=p->next;
x=p->data;
printf("%d has been popped\n",x);
free(p);
}
//display
void display()
{
stack *p;
for(p=top;p!=NULL;p=p->next)
printf("%d\n",p->data);
}
//size
int size()
{
stack *p;
int i;
for(i=0,p=top;p!=NULL;i++,p=p->next);
return i;
}
int main()
{
int choice,x,k;
do{
printf("\n-----menu----");
printf("\n 1.push\n 2.pop\n 3.display\n 4.size\n 5.exit\n ");
printf("enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the element to be entered: ");
scanf("%d",&x);
push(x);
break;
case 2: pop();
break;
case 3:
display();
break;
case 4:
k=size();
printf("size is %d\n",k);
break;
case 5:
printf("exited\n");
break;
default :
printf("invalid choice\n");
}
}while(choice!=5);
}