-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathstack_using_doubly_ll.c
210 lines (101 loc) · 2.9 KB
/
stack_using_doubly_ll.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node* next;
struct Node* prev;
}node;
node* alloc(int val){//Allocate node for Doubly Linked List
node* newnode = (node*)malloc(sizeof(node));
newnode->next = NULL;
newnode->prev = NULL;
newnode->data = val;
return newnode;
}
node* push(node* head,int val){//Push in to the front of list
if(head == NULL){//Handle case for first insertion
node* newnode = alloc(val);
head = newnode;
}
else{//Push into the front and readjust head after pointing previous of head to newnode
node* newnode = alloc(val);
newnode->next = head;
head->prev = newnode;
head = newnode;
}
return head;
}
void traverse(node* head){//Traverse the list until NULL
node* current = head;
if(head==NULL){//If no node present
printf("Underflow - no nodes present\n");
return;
}
printf("The current stack is \n");
while(current!=NULL){//While not NULL traverse the list
printf("%d ",current->data);
current = current->next;
}
printf("\n");
}
node* pop(node* head){
if(head==NULL){//Handle when no nodes present
printf("Underflow - no nodes present\n");
}
else if(head->next==NULL){//Handle when only one node present
node* temp = head;
head = NULL;
free(temp);
}
else{//Else move the head by one node
node* temp = head;
head = head->next;
head->prev = NULL;
free(temp);
}
return head;
}
void peek(node* head){//Show the topmost element
if(head==NULL){//When no node present
printf("Underflow - no nodes present\n");
}
else {//Show the data of head
printf("The topmost element of the stack is : %d\n",head->data);
}
}
int main(){
node* head=NULL;
long int i,n,k;
printf("-------- Choose any number from the below sequence -------------\n");
printf(" 1. Insert into stack \n");
printf(" 2. Delete from stack \n");
printf(" 3. Peek \n");
printf(" 0. exit\n");
printf("----------------------------------------------------------------\n");
while(1){
printf("Enter next choice : ");
scanf("%ld",&i);
switch(i){
case 0:
break;
case 1:
printf("Enter a value to push into stack : ");
scanf("%ld",&n);
head = push(head,n);
traverse(head);
break;
case 2:
head = pop(head);
traverse(head);
break;
case 3:
peek(head);
break;
default:
printf("Enter a valid choice");
}
if(i==0)
break;
}
return 0;
}