-
Notifications
You must be signed in to change notification settings - Fork 0
/
trabalho_g2_eduardo_dusik.cpp
181 lines (149 loc) · 3.02 KB
/
trabalho_g2_eduardo_dusik.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <malloc.h>
struct nodo {
int dados;
struct nodo *proximo;
};
void push (struct nodo **topo, int valor){
struct nodo *p;
p = (nodo *)malloc(sizeof(struct nodo));
p->dados = valor;
p->proximo = *topo;
*topo = p;
}
int pop (struct nodo **topo){
int valor;
struct nodo *p;
valor = (*topo)->dados;
p = *topo;
*topo = p->proximo;
free(p);
return valor;
}
void mostrar_pilha(struct nodo **fila){
struct nodo *aux;
aux=*fila;
while (aux != NULL)
{
printf("Valores-> %i\n",aux->dados);
aux=aux->proximo;
}
}
void retira_3(struct nodo **pilha1, int cont){
struct nodo *aux1;
aux1 = NULL;
while(cont > 0){
if((*pilha1)->dados % 3 ==0){
pop(pilha1);
cont--;
}else if (cont != 0){
push(&aux1, (*pilha1)->dados);
pop(pilha1);
cont--;
}
}
*pilha1 = aux1;
}
void enfileirar (nodo **fila, int valor) {
struct nodo *auxi;
struct nodo *x = (nodo *)malloc(sizeof(struct nodo));
x->dados = valor;
x->proximo = NULL;
if(*fila == NULL){
*fila = x;
}else {
auxi = *fila;
while (auxi->proximo != NULL){
auxi = auxi->proximo;
}
auxi->proximo = x;
}
}
int desenfileirar (nodo **fila){
int valor;
struct nodo *aux;
aux = *fila;
valor = aux->dados;
*fila = aux->proximo;
free(aux);
return valor;
}
void maior_10(struct nodo **fila1, struct nodo **fila2, struct nodo **fila3){
struct nodo *aux1;
struct nodo *aux2;
aux1 = *fila1;
aux2 = *fila2;
while(aux1 != NULL){
if(aux1->dados > 10){
enfileirar(fila3, aux1->dados);
aux1 = aux1->proximo;
}else {
aux1 = aux1->proximo;
}
}
while(aux2 != NULL){
if(aux2->dados > 10){
enfileirar(fila3, aux2->dados);
aux2 = aux2->proximo;
}else {
aux2 = aux2->proximo;
}
}
}
void filas (){
struct nodo *fila1;
struct nodo *fila2;
struct nodo *fila3;
int valor = 1;
while(valor !=0){
printf("Digite um valor para armazenar na fila 1 (0 para sair): ");
scanf("%i", &valor);
if(valor != 0){
enfileirar(&fila1, valor);
}
}
valor = 1;
while(valor != 0){
printf("Digite um valor para armazenar na fila 2 (0 para sair): ");
scanf("%i", &valor);
if(valor !=0){
enfileirar(&fila2, valor);
}
}
mostrar_pilha(&fila3);
}
void pilhas () {
struct nodo *pilha1;
int valor=1;
int cont = 0;
while(valor != 0){
printf("Digite um valor para armazenar na pilha 1 e zero (0): ");
scanf("%i", &valor);
if(valor !=0) {
cont++;
push(&pilha1, valor);
}
}
retira_3(&pilha1, cont);
mostrar_pilha(&pilha1);
}
int main () {
int resposta = 1;
while(resposta != 0){
printf("Digite uma opcao:\n1 - Pilhas\n2 - Filas\n\nSua opcao: ");
scanf("%i", &resposta);
switch (resposta){
case 1:
pilhas();
break;
case 2:
filas();
break;
case 0:
printf("Voce escolheu sair :(");
break;
default:
printf("Escolha uma opcao: ");
}
}
}