-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedStack.c
More file actions
96 lines (68 loc) · 1.56 KB
/
linkedStack.c
File metadata and controls
96 lines (68 loc) · 1.56 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
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
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
struct node {
int value;
struct node *next;
};
typedef struct node _node;
_node *addElement(int element, _node *root) {
_node *newNode;
newNode = (_node *) malloc(sizeof(_node));
if (newNode != NULL) {
newNode->value = element;
newNode->next = root;
root = newNode;
}
return root;
}
_node *removeElement(_node *root) {
_node *nodeToRemove;
if (root != NULL) {
nodeToRemove = root;
root = root->next;
free(nodeToRemove);
}
return root;
}
void print(_node *root) {
_node *aux;
aux = root;
printf("stack:\n");
while (aux != NULL) {
printf("[%d]\n", aux->value);
aux = aux->next;
}
printf("\n");
}
int size(_node *root) {
_node *aux;
int size = 0;
aux = root;
if (root != NULL) {
while (aux != NULL) {
size++;
aux = aux->next;
}
}
return size;
}
int main() {
_node *root;
root = NULL;
root = addElement(5, root);
root = addElement(11, root);
root = addElement(3, root);
root = addElement(2, root);
root = addElement(1, root);
root = addElement(9, root);
root = addElement(50, root);
root = addElement(30, root);
root = addElement(53, root);
print(root);
printf("size: %d\n", size(root));
root = removeElement(root);
print(root);
return EXIT_SUCCESS;
}