-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
77 lines (61 loc) · 1.58 KB
/
stack.c
File metadata and controls
77 lines (61 loc) · 1.58 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
#include <stdbool.h>
#include <stdlib.h>
#include "stack.h"
/* - STRUCTS - */
// Nodes contain stored data and a pointer to the next node.
struct node {
char character;
struct node *next;
};
/* - FUNCTIONS - */
// Creates a blank node that acts as the accessor for the stack structure. The
// returned pointer will be NULL if malloc fails to allocate this node.
Node *make_stack() {
Node *new_stack = (Node *)malloc(sizeof(Node));
if (new_stack) {
new_stack->character = '\0';
new_stack->next = NULL;
}
return new_stack;
}
// Adds a node to the top of the stack containing the value c. Returns false if
// the stack is NULL.
bool push(Node *stack, char c) {
if (!stack) return false;
Node *node = (Node *)malloc(sizeof(Node));
if (!node) return false;
node->next = stack->next;
node->character = c;
stack->next = node;
return true;
}
// Removes node at top of stack and returns its stored value
char pop(Node *stack) {
if (stack && stack->next) {
Node *temp = stack->next;
stack->next = temp->next;
char return_value = temp->character;
free(temp);
return return_value;
}
return '\0';
}
// Returns the value at the top of the stack without removing the node.
char peak(Node *stack) {
if (stack && stack->next) {
return stack->next->character;
}
return '\0';
}
// Frees every node in the stack structure. Returns false if the stack is NULL.
bool free_stack(Node *stack) {
if (!stack) return false;
Node *temp = stack;
while (stack) {
temp = stack->next;
free(stack);
stack = temp;
// printf("free node\n");
}
return true;
}