-
Notifications
You must be signed in to change notification settings - Fork 22
/
_10.StackUsingLL.c
96 lines (88 loc) · 1.81 KB
/
_10.StackUsingLL.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
92
93
94
95
96
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* top = NULL;
struct node* Push()
{
struct node* new = (struct node*)malloc(sizeof(struct node));
printf("\nEnter the element to be pushed into the stack:");
scanf("%d",&new->data);
new->next = top;
top = new;
return top;
}
struct node* Pop()
{
if(top == NULL)
{
printf("\n*****STACK UNDERFLOW*****\n");
return top;
}
else
{
struct node* temp = top;
printf("\nElement being popped is %d",temp->data);
top = top->next;
free(temp);
}
return top;
}
void Display()
{
if(top==NULL)
{
printf("\n*****EMPTY STACK*****\n");
}
else
{
printf("\nStack elements:\n");
for(struct node* i = top; i != NULL; i=i->next)
{
printf("%d\t",i->data);
}
}
}
int main()
{
int flag = 0,choice;
printf("\n*****IMPLEMENTATION OF STACK USING LINKED LISTS*****\n");
while(flag==0)
{
printf("\n-----------------------------------------------------\n");
printf("\nEnter your choice:\n\t1.Push\n\t2.Pop\n\t3.Display\n\t4.Exit\nChoice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
top=Push(top);
break;
}
case 2:
{
top=Pop(top);
break;
}
case 3:
{
Display(top);
break;
}
case 4:
{
flag=1;
break;
}
default:
{
printf("\nINVALID CHOICE\n");
break;
}
}
}
return 0;
}