-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
114 lines (103 loc) · 2.22 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// stack.c
// expr
//
// Created by sallaben on 10/9/17.
//
#include "stack.h"
/*
* Stack struct definition
*/
struct Stack {
int list[64];
int size;
};
/*
* Allocate and initialize a new instance of a stack (with a limit of 64 integers).
*/
Stack Stack_new() {
Stack stack = malloc(sizeof(struct Stack));
stack->size = 0;
return stack;
}
/*
* Adds a new integer to the top of the stack
*/
void Stack_push(Stack stack, int i) {
Stack_shift_right(stack);
stack->list[0] = i;
stack->size++;
}
/*
* Returns and removes the current top integer of the stack
*/
int Stack_pop(Stack stack) {
int i = stack->list[0];
Stack_shift_left(stack);
stack->size--;
return i;
}
/*
* Returns the current top integer of the stack
*/
int Stack_peek(Stack stack) {
if(stack->size > 0) {
return stack->list[0];
} else {
return -1;
}
}
/*
* Returns the current integer size of the stack
*/
int Stack_size(Stack stack) {
return stack->size;
}
/*
* Prints the given stack in order, distinguishing the top element with [brackets]
*/
void Stack_print(Stack stack) {
int size = Stack_size(stack);
printf("\n\nPrinting Stack of size %d", size);
if(size > 0) {
for(int i = 0; i < size; i++) {
if(i == 0) {
printf("\n[%c] (%d)", stack->list[0], stack->list[0]);
} else {
printf("\n%c (%d)", stack->list[i], stack->list[i]);
}
}
} else {
printf("\nNothing to print!");
}
}
/*
* "Shifts" the given stack 1 space to the left, removing space left behind from Stack_pop()
*/
bool Stack_shift_left(Stack stack) {
if(stack->size <= 0) {
return false;
}
for(int i = 1; i < stack->size; i++) {
stack->list[i - 1] = stack->list[i];
}
return true;
}
/*
* "Shifts" the given stack 1 space to the right, creating space for the new integer given by Stack_push()
*/
bool Stack_shift_right(Stack stack) {
if(stack->size >= 64) {
return false;
}
for(int i = stack->size; i > 0; i--) {
stack->list[i] = stack->list[i - 1];
}
return true;
}
/*
* Frees the Stack from memory
*/
void Stack_free(Stack stack) {
free(stack);
}