-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblema_1.c
89 lines (68 loc) · 1.57 KB
/
problema_1.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
#include<stdio.h>
#include<stdlib.h>
typedef struct _no{
int cons;
struct _no *prox;
}no;
void push(int v, no **ini, no **fim){
no *aux;
if((*ini)==NULL){
*ini = (no *) malloc(sizeof(no));
(*ini)->prox = NULL;
(*ini)->cons = v;
*fim=*ini;
} else {
aux = (no *) malloc(sizeof(no));
aux->cons = v;
aux->prox = NULL;
(*fim)->prox = aux;
*fim = aux;
}
}
int pop(no **ini, no **fim){
int ret;
no *aux;
if((*ini == NULL) && (*fim == NULL)){
return -1;
} else {
if((*ini) == (*fim)){
aux = *ini;
*ini = (*ini)->prox;
*fim = *ini;
ret = aux->cons;
free(aux);
return ret;
} else {
aux =* ini;
(*ini) = (*ini)->prox;
ret = aux->cons;
free(aux);
return ret;
}
}
}
void print(no **ini){
no *aux = NULL;
aux = *ini;
if(aux != NULL){
while(aux != NULL){
printf("%d\n", aux->cons);
aux = aux->prox;
}
}
}
int main(){
int qdt, i, v, ret;
no *ini = NULL, *fim=NULL;
scanf("%d",&qdt);
for(i=0; i<qdt; i++){
scanf("%d",&v);
push(v, &ini, &fim);
}
while (ini->prox != NULL){
pop(&ini, &fim);
ret = pop(&ini, &fim);
push(ret, &ini, &fim);
}
print(&ini);
}