forked from 0xali3n/Hactoberfest-Beginner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack using link list.c
91 lines (85 loc) · 1.45 KB
/
Stack using link list.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>
struct node
{
int data;
struct node *link;
};
struct node* push(struct node *top,int value)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->link=NULL;
if(top==NULL)
{
top=temp;
return top;
}
else{
temp->link=top;
top=temp;
return top;
}
}
struct node* pop(struct node *top)
{
struct node *temp;
if(top==NULL)
{
printf("stack is empty!\n");
return top;
}
else{
temp=top;
printf("poped element is: %d\n",temp->data);
top=top->link;
free(temp);
return top;
}
}
void display(struct node *top)
{
struct node *p;
p=top;
if(top==NULL)
{
printf("stack is empty!\n");
}
printf("The stack is:\n")
while(p->link!=NULL)
{
printf("%d->",p->data);
p=p->link;
}
printf("%d\n",p->data);
}
int main(void)
{
int n,ch,value;
struct node *top=NULL;
while(1)
{
printf("stack menu is:\n\n");
printf("1. push element\n");
printf("2. pop element\n");
printf("3. display\n");
printf("4. exit\n\n");
printf("Enter your choice(1-4):\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the value to push:\n");
scanf("%d",&value);
top=push(top,value);
break;
case 2: top=pop(top);
break;
case 3: display(top);
break;
case 4: exit(0);
default: printf("Wrong choice!\n");
}
}
return 0;
}