-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
111 lines (99 loc) · 2.62 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
/**
* Implementation of stack data structure.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
Stack *initStack(int (*destroy)(void *data), char *(*toString)(void *data),
int (*compare)(void *key1, void *key2)) {
Stack *newStack;
if ((newStack = malloc(sizeof(Stack))) == NULL) {
fprintf(stderr, "ERROR: Couldn't malloc for new Stack\n");
newStack = NULL;
}
else {
newStack->size = 0;
newStack->destroy = destroy;
newStack->toString = toString;
newStack->compare = compare;
newStack->head = NULL;
}
return newStack;
}
StackItem *initStackItem(void *data) {
StackItem *newItem;
if ((newItem = malloc(sizeof(StackItem))) == NULL) {
fprintf(stderr, "ERROR: Couldn't malloc for new StackItem\n");
newItem = NULL;
}
else {
newItem->data = data;
newItem->previous = NULL;
}
return newItem;
}
void push(Stack *stack, void *data) {
StackItem *newItem;
if ((newItem = initStackItem(data)) == NULL) {
fprintf(stderr, "ERROR: Couldn't push item onto Stack\n");
}
else {
newItem->previous = stack->head;
stack->head = newItem;
(stack->size)++;
}
}
void *pop(Stack *stack) {
StackItem *off;
if (stack->size == 0) {
fprintf(stderr, "ERROR: Stack is empty; can't pop anything off\n");
off = NULL;
}
else {
off = stack->head;
stack->head = off->previous;
(stack->size)--;
}
void *offdata = off->data;
free(off);
return offdata;
}
void *peekStack(Stack *stack) {
StackItem *top;
if (stack->size == 0) {
fprintf(stderr, "ERROR: Stack is empty; can't peek top item\n");
top = NULL;
}
else {
top = stack->head;
}
void *topdata = top->data;
return topdata;
}
StackItem *freeStackItem(Stack *stack, StackItem *stackItem) {
destroyStackItem(stack, stackItem);
StackItem *previous = stackItem->previous;
free(stackItem);
return previous;
}
void freeStack(Stack *stack) {
StackItem *itemPtr = stack->head;
while (itemPtr != NULL) {
itemPtr = freeStackItem(stack, itemPtr);
(stack->size)--;
}
free(stack);
}
void stackItemPrint(Stack *stack, StackItem *stackItem) {
printf("%s\n", (stack->toString)(stackItem->data));
}
void printStackTop(Stack *stack) {
stackItemPrint(stack, stack->head);
}
void stackPrint(Stack *stack) {
StackItem *stackPtr = stack->head;
for (stackPtr; stackPtr != NULL; stackPtr = stackPtr->previous)
stackItemPrint(stack, stackPtr);
}