-
Notifications
You must be signed in to change notification settings - Fork 1
/
StackImplementationUsingLinkedList.c
114 lines (103 loc) · 1.78 KB
/
StackImplementationUsingLinkedList.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node *list = NULL;
struct node* getnode()
{
return (struct node*)malloc(sizeof(struct node));
}
void push(int x)
{
if(list == NULL)
{
struct node *nn;
nn = getnode();
nn->info = x;
nn->next = NULL;
list = nn;
}
else
{
struct node *nn, *temp;
nn = getnode();
temp = list;
nn->info = x;
nn->next = NULL;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = nn;
}
}
void pop()
{
if(list == NULL)
{
printf("Stack is Empty!\n");
}
else
{
struct node *temp, *prev;
temp = list;
while(temp->next!=NULL)
{
prev = temp;
temp = temp->next;
}
prev->next = NULL;
printf("%d got popped!\n",temp->info);
}
}
void show()
{
if(list == NULL)
{
printf("Stack is Empty!\n");
}
else
{
struct node *temp;
temp = list;
while(temp!=NULL)
{
printf("%d\n", temp->info);
temp = temp->next;
}
}
}
int main()
{
int ch, n;
while(1) {
printf("\n------stack_menu-------");
printf("\n1. push");
printf("\n2. pop");
printf("\n3. show");
printf("\n4. Exit");
printf("\nEnter Your Choice : ");
scanf("%d", &ch);
switch(ch){
case 1:
printf("Enter a Number to push : ");
scanf("%d", &n);
push(n);break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Choice! Try Again!");
}
}
return 0;
}