-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathqueue.c
107 lines (90 loc) · 1.6 KB
/
queue.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
#include <stdio.h>
#include <stdlib.h>
//This prog teachs how to use structs of Queue in c
//Main struct of all structs
typedef struct no
{
struct no * next;
int n;
}NODE;
//expecific struct used in queue programs
typedef struct fila
{
NODE * begin,*final;
}FILA;
//auxiliar functions
NODE * cria_no(int n);
void print_no(NODE * l);
//Queue progs
FILA * cria_fila(void);
FILA * insert_fila(FILA * f,int n);
FILA * delete_fila (FILA * f);
int main (){
int n,i;
FILA * fila = cria_fila();
while(scanf("%d",&n)!=EOF){
fila = insert_fila(fila,n);
}
printf("OK\n");
while(scanf("%d",&n)!=EOF){
delete_fila(fila);
print_no(fila->begin);
}
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");
}
}
///***FILAS*****
FILA * cria_fila(void){
FILA * f = malloc(sizeof(FILA));
if (f!=NULL){
f->begin = f->final = NULL;
return f;
}
exit(1);
}
FILA * insert_fila(FILA * f,int n){
if (f->begin == NULL){
f->begin = f->final = cria_no(n);
}else{
NODE * aux = cria_no(n);
aux->next = f->begin;
f->begin = aux;
}
return f;
}
FILA * delete_fila (FILA * f) {
if (f->begin == NULL) return f;
else{
if (f->begin == f->final){
NODE * aux = f->begin;
f->begin = f->final = NULL;
free(aux);
}else{
NODE * aux = f->begin;
while(aux->next!=f->final){
aux = aux->next;
}
f->final = aux;
aux = aux->next;
free(aux);
f->final->next = NULL;
}
}
return f;
}