-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
64 lines (58 loc) · 1.39 KB
/
stack.c
File metadata and controls
64 lines (58 loc) · 1.39 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int val;
struct node* next;
}node;
void pop(node** list_start);
void push(node** list_start);
void printstack(node* list_start);
int main()
{
node* list_start = NULL;
while(1)
{
int inp;
printf("\nWhat do you want to do with the stack:\n1) pop\n2) push\n3) print stack\n4) exit\n>");
scanf("%d", &inp);
switch(inp)
{
case 1: pop(&list_start); break;
case 2: push(&list_start); break;
case 3: printstack(list_start); break;
case 4: printf("Closing program...\n"); return 0; break;
}
}
}
void printstack(node* list_start)
{
node* current_node = list_start;
printf("Items in stack (top to bottom): ");
while(current_node != NULL)
{
printf("%d ", current_node -> val);
current_node = current_node -> next;
}
}
void pop(node** list_start)
{
if(*list_start == NULL)
{
printf("No values in stack!");
return;
}
node* current_node = *list_start;
printf("Popped value: %d", current_node -> val);
*list_start = current_node -> next;
free(current_node);
}
void push(node** list_start)
{
node* new = malloc(sizeof(node));
printf("Value to push: ");
scanf("%d", &(new -> val));
new -> next = *list_start;
*list_start = new;
printf("Pushed!\n");
}