This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp19_1511.c
98 lines (84 loc) · 1.52 KB
/
p19_1511.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
// Emmanuel Jojy
// S3 CSE A
// Roll no: 53
// Prg 19/1511
// Stack Implementation
// Dynamic Memory Allocation
#include<stdio.h>
#include<stdlib.h>
void push();
void pop();
void display();
void exit_free();
struct node{
int data;
struct node *link;
};
struct node *TOP = NULL;
struct node *temp;
void main(){
int ch;
while(1){
printf("\n\n*** STACK OPTIONS ***");
printf("\n1. PUSH 2. POP 3. DISPLAY 4. EXIT");
printf("\nEnter choice: ");
scanf("%d", &ch);
switch(ch){
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
default: exit_free(); return;
}
}
}
// Insertion at beginning - PUSH
// Choice 1
void push(){
int d;
struct node *a = malloc(sizeof(struct node));
printf("\tEnter data: ");
scanf("%d", &(a->data));
a->link = TOP;
TOP = a;
printf("\tSuccessfully Pushed %d", a->data);
}
// Deletion from beginning - POP
// Choice 2
void pop(){
if(TOP == NULL){
printf("\tStack Underflow");
}
else{
temp = TOP;
TOP = TOP->link;
printf("\tSuccessfully Popped %d", temp->data);
free(temp);
}
}
// Displaying all nodes
// Choice 3
void display(){
if(TOP == NULL){
printf("\tStack Underflow");
return;
}
printf("\tCuurent STACK >>>");
for(temp = TOP; temp != NULL; temp = temp->link){
printf("\n\t%d", temp->data);
}
}
// Final de-allocation of SLL
// Choice 4
void exit_free(){
struct node *loc;
if(TOP != NULL){
temp = TOP;
loc = temp->link; // Next node
while(loc != NULL){
free(temp);
temp = loc;
loc = loc->link;
}
free(temp);
}
}