-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.c
80 lines (71 loc) · 1.54 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
#include <stdio.h>
#include <stdlib.h>
#include "Headers/stack.h"
Stack* createStack() {
Stack *stack = (Stack*) malloc(sizeof (Stack));
return stack;
}
void initializeStack(Stack* stack) {
stack->length = 0;
stack->top = NULL;
}
int push(Stack *stack, ItemType e) {
Node *node = (Node*) malloc(sizeof (Node));
node->data = e;
node->next = stack->top;
if (stack->length == 0) {
node->next = NULL;
}
stack->top = node;
stack->length++;
return 1;
}
int pop(Stack* stack, ItemType* e) {
if (stack->top) {
Node *node = (Node*) malloc(sizeof (Node));
node = stack->top;
stack->top = node->next;
*e = node->data;
free(node);
stack->length--;
return 1;
}
return 0;
}
int peek(Stack* stack, ItemType* e) {
if (stack->top) {
*e = stack->top->data;
return 1;
}
return 0;
}
int contains(Stack* stack, ItemType* e) {
Node *node = stack->top;
while (node) {
if (node->data == *e) {
return 1;
}
node = node->next;
}
return 0;
}
int sizeStack(Stack* stack) {
return stack->length;
}
int isEmptyStack(Stack* stack) {
return stack->length == 0;
}
void printStack(Stack* stack) {
int i = stack->length - 1;
Node *node = stack->top;
printf("\n");
printf("#\tItem\tEndereço\n");
while (node) {
printf("%d\t", i);
printf("%c\t", node->data);
printf("%p\n", node);
node = node->next;
i--;
}
printf("\n");
}