-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
76 lines (63 loc) · 1.29 KB
/
stack.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct node_value {
char name[256];
struct node_value *prev;
} node;
node *top;
node *build_node() {
return malloc(sizeof(node));
}
node *read_node() {
node *new_node = build_node();
new_node->prev = NULL;
printf("Provide a name: ");
scanf("%[^\n]%*c", new_node->name);
return new_node;
}
void push(node *new_node) {
if(top == NULL) {
top = new_node;
return;
}
new_node->prev = top;
top = new_node;
}
node *pop() {
if(top == NULL) {
return NULL;
}
node *pop = build_node();
strcpy(pop->name, top->name);
node *to_free = top;
top = top->prev;
free(to_free);
return pop;
}
void print_node(node *n) {
printf("Name: %s\n", n->name);
}
void print_stack() {
printf("\nbegin of stack...\n");
node *actual = top;
while(actual != NULL) {
print_node(actual);
actual = actual->prev;
}
printf("...end of stack\n\n");
}
int main(int argc, char *argv[]) {
push(read_node());
push(read_node());
push(read_node());
print_stack();
print_node(pop());
print_stack();
print_node(pop());
print_stack();
print_node(pop());
print_stack();
push(read_node());
print_stack();
}