-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathlinked_list_stack_C.c
141 lines (116 loc) · 2.49 KB
/
linked_list_stack_C.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
// author-akshat khatri
// date-12-08-2023
// stack implementation using linked list
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct list_node
{
int data;
struct list_node *next;
};
struct list_node *newnode(int data)
{
struct list_node *stack_node = (struct list_node *)malloc(sizeof(struct list_node));
stack_node->data = data;
stack_node->next = NULL;
return stack_node;
}
int is_empty(struct list_node *root)
{
if (root == NULL)
{
return 1;
}
else
{
return 0;
}
}
void push(struct list_node **root, int data)
{
struct list_node *stack_node = newnode(data);
stack_node->next = *root;
*root = stack_node;
printf("element %d pushed to stack", data);
}
void pop(struct list_node **root)
{
if (is_empty(*root))
{
printf("severe underflow condition\n");
return;
}
struct list_node *temp = *root;
*root = (*root)->next;
printf("element popped out is %d\n", temp->data);
free(temp);
return;
}
void peek(struct list_node *root)
{
if (is_empty(root))
{
printf("severe underflow condition\n");
return;
}
printf("element peeked out was %d\n", root->data);
}
void display_stack(struct list_node *root)
{
if (is_empty(root))
{
printf("stack is empty\n");
}
else
{
struct list_node *temp = root;
while (temp->next != NULL)
{
printf("|%d|\n", temp->data);
temp = temp->next;
}
printf("|%d|",temp->data);
}
}
int main()
{
struct list_node *root = NULL;
int choice, element;
do
{
printf("\npress 1 for pushing an element in the stack\npress 2 for popping an element from the stack\npress 3 for peeking an element from the stack\npress 4 to display the stack\n press 0 to exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("enter the element you want to push in the stack\n");
scanf("%d", &element);
push(&root, element);
break;
case 2:
pop(&root);
break;
case 3:
peek(root);
break;
case 4:
display_stack(root);
}
} while (choice != 0);
printf("exited program succesfully\n");
return 0;
}
/**
* *hello world
* !the method is good
* ? is this useful
* TODO je
*
*/
/**
* !the unthinkable
* *oh really
* ?yes really?
* *done
*/