-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack-with-linked-list.c
95 lines (74 loc) · 1.38 KB
/
stack-with-linked-list.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
#include<stdio.h>
#include<stdlib.h>
#include "lib-linked-list.h"
int QUIT_CMD = 9;
void empty_msg()
{
printf(" Stack is empty.\n");
}
int value_from_stdin()
{
printf(" Enter your value: ");
int value;
scanf("%d", &value);
return value;
}
Node* init_stack()
{
return create_linked_list();
}
Node* add_to_stack(Node *head)
{
Node *newnode = new_node();
newnode->data = value_from_stdin();
push_front(head, newnode);
return newnode;
}
Node* pop_from_stack(Node *head)
{
if (head == NULL)
{
empty_msg();
return NULL;
}
return pop_front(head);
}
void print(Node *head)
{
if (head == NULL)
{
return empty_msg();
}
print_linked_list(head);
}
int main(void) {
int choice;
Node *stack = NULL;
do
{
printf("0. Print\n");
printf("1. Add to stack\n");
printf("2. Pop from stack\n");
printf("9. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 0:
printf("Printing the stack...\n");
print(stack);
break;
case 1:
printf("Adding to stack...\n");
Node *add_result = add_to_stack(stack);
if (add_result != NULL) {
stack = add_result;
}
break;
case 2:
printf("Popping from stack...\n");
stack = pop_from_stack(stack);
break;
}
}
while (choice != QUIT_CMD);
};