-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathstacks.c
80 lines (63 loc) · 1.11 KB
/
stacks.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>
//This prog teachs how to use structs of Stacks in c
//Main struct of all structs
typedef struct no
{
struct no * next;
int n;
}NODE;
//auxiliar functions
NODE * cria_no(int n);
void print_no(NODE * l);
//stacks progs
NODE * insert_pilha (NODE * p,int n);
NODE * delete_pilha (NODE * p);
int main (){
int n,i;
NODE * pilha = NULL;
while(scanf("%d",&n)!=EOF){
pilha = insert_lista (pilha,n);
getchar();
}
printf("Numeros inseridos\n");
print_no(pilha);
while(scanf("%d",&n)!=EOF){
lista = delete_pilha (lista,n);
print_no(pilha);
}
return 0;
}
NODE * cria_no(int n){
NODE * new = malloc(sizeof(NODE));
if (new!= NULL){
new->next = NULL;
new->n = n;
return new;
}
exit(1);
}
void print_no(NODE * l){
if (l !=NULL){
printf("%d ",l->n );
print_no(l->next);
}else {
printf("\n");
}
}
///***PILHA*****
NODE * insert_pilha (NODE * p,int n){
if (p == NULL) return cria_no(n);
else{
NODE * aux = cria_no(n);
aux->next = p;
return aux;
}
}
NODE * delete_pilha (NODE * p){
if (p!=NULL){
NODE * aux = p;
p = p->next;
free(aux);
}
}