-
Notifications
You must be signed in to change notification settings - Fork 0
/
8-Ada_copy.c
150 lines (120 loc) · 3.17 KB
/
8-Ada_copy.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Item;
typedef struct no_st{
Item item;
struct no_st *prox;
struct no_st *ant;
}No;
typedef struct header_St{
No *inicio;
No *final;
int no_count;
}Header;
int initialize_queue(Header *H){
H -> inicio = NULL;
H -> final = NULL;
return 1;
}
int is_empty(Header *H){return H->inicio == NULL && H->final == NULL;}
void front(Header *H){
if(is_empty(H))return printf("No job for Ada?\n");
printf("%d\n", H -> inicio -> item);
if (H->inicio == H->final && H->inicio != NULL) {
H -> final = NULL;
H -> inicio = NULL;
}
else {
H -> inicio = H -> inicio -> prox;
}
}
void back(Header *H){
if(is_empty(H))return printf("No job for Ada?\n");
printf("%d\n", H -> final -> item);
if(H -> inicio == H -> final && H->inicio != NULL){
H -> inicio = NULL;
H -> final = NULL;
} else{
H->final = H->final->ant;
//H->final->prox = NULL;
}
}
int toFront(Header *H, Item insert){
No *temp = malloc(sizeof(No));
if(temp == NULL)return 0;
temp->item = insert;
temp->prox = NULL;
temp->ant = NULL;
if (H->inicio == NULL && H->final == NULL) {
H->inicio = temp;
H->final = H->inicio;
}
else {
H->inicio->ant = temp;
temp->prox = H->inicio;
H->inicio = temp;
}
return 1;
}
int push_back(Header *H, Item insert){
No *temp = malloc(sizeof(No));
if (temp == NULL)return 0;
temp->item = insert;
temp->prox = NULL;
temp->ant = NULL;
if (H->final == NULL || H->inicio == NULL) {
H->final = temp;
H->inicio = H->final;
}
else {
H->final->prox = temp;
temp->ant = H->final;
H->final = temp;
}
return 1;
}
int main() {
int instrucoes;
scanf("%d", &instrucoes);
Header h;
initialize_queue(&h);
int reverse = 0;
for (int i = 0; i < instrucoes; i++) {
char instrucao[9]; // trocar pra 10 se der erro
int queue;
scanf("%s", instrucao);
if (!strcmp(instrucao, "reverse")) {
if (reverse == 0) {
reverse = 1;
}
else if (reverse == 1) {
reverse = 0;
}
}
if (reverse == 0) {
if (!strcmp(instrucao, "toFront")){
scanf("%d", &queue);
toFront(&h, queue);
}
if (!strcmp(instrucao, "front")){front(&h);}
if (!strcmp(instrucao, "push_back")){
scanf("%d", &queue);
push_back(&h, queue);
}
if (!strcmp(instrucao, "back")){back(&h);}
}
if (reverse == 1) {
if (!strcmp(instrucao, "toFront")){
scanf("%d", &queue);
push_back(&h, queue);
}
if (!strcmp(instrucao, "front")){back(&h);}
if (!strcmp(instrucao, "push_back")){
scanf("%d", &queue);
toFront(&h, queue);
}
if (!strcmp(instrucao, "back")){front(&h);}
}
}
}