-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
71 lines (60 loc) · 1.05 KB
/
stack.c
File metadata and controls
71 lines (60 loc) · 1.05 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
#include <stdio.h>
#include <stdlib.h>
typedef struct Bloco{
int data;
struct Bloco* prox;
} Bloquinho;
typedef struct Node{
Bloquinho* topo;
} No;
No* aloca_no(){
No* n = (No*) malloc(sizeof(No));
n->topo = NULL;
return n;
}
Bloquinho* aloca_bloquinho(int valor){
Bloquinho* b = (Bloquinho*) malloc(sizeof(Bloquinho));
b->data = valor;
b->prox = NULL;
return b;
}
int esta_vazia(No* Node){
return (Node->topo == NULL);
}
void push(char valor, No* Node){
Bloquinho* b = aloca_bloquinho(valor);
b->prox = Node->topo;
Node->topo = b;
}
void pop(No* Node){
if(esta_vazia(Node)){
printf("Ocorreu underflow\n");
}
else{
Bloquinho* temp;
temp = Node->topo;
char aux = temp->data;
Node->topo = Node->topo->prox;
free(temp);
}
}
int top(No* Node){
return Node->topo->data;
}
void print(No* Node){
Bloquinho* b = Node->topo;
while(b != NULL){
printf("%d\n", b->data);
b = b->prox;
}
}
int main(){
No* Node = aloca_no();
push(7, Node);
push(9, Node);
push(10, Node);
print(Node);
printf("********\n");
pop(Node);
print(Node);
}