1
Consider a storage area of 5 nodes. Over this area will be mounted a list simply chained with ascending ordering by the INFO field, named LIST. Initially create PND and NODO-CABEÇA to make this list operational. Then, display the behavior of the list during the following operations:
- Inclusion of GREEN information
- Inclusion of RED information
- Inclusion of ORANGE information
- Withdrawal of GREEN information
- Inclusion of OCRE information
- Inclusion of ROSA information
- Removal of first information in logical order
- Inclusion of BLACK information
- Inclusion of GREY information
- Withdrawal of information RED
- Withdrawal of information BLACK
- Removal of the last information in logical order
- Inclusion of WHITE information
- Inclusion of LILAC information
- Withdrawal of information GRAY
- Withdrawal of information WHITE
My code does not give any build error, but the result is not what I expected. Could anyone help me? Follow my code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAMANHO 5
struct nodo {
char info[8];
int elo;
};
int disp, nc;
struct nodo lista[TAMANHO];
void cria_pnd(void) {
int i = 0;
while(i < TAMANHO -1) {
lista[i].elo = i+1;
i++;
}
lista[TAMANHO-1].elo = -1;
disp = 0;
}
int obtem(void) {
int aux;
if(disp == -1)
return(-1);
aux = disp;
disp = lista[disp].elo;
return(aux);
}
void cria_nc(void) {
nc = obtem();
if(nc == -1) {
printf("\nNao pode criar NC. Programa abortado!");
fflush(stdin);
getchar();
exit(1);
}
strcpy(lista[nc].info , "-1");
lista[nc].elo = -1;
}
void libera(int indice) {
lista[indice].elo = disp;
disp = indice;
}
void inclusao(char valor) {
int post, ant, indice;
indice = obtem();
if(indice == -1)
printf("\nOVERFLOW");
else {
strcpy(lista[indice].info , "valor");
ant = nc;
post = lista[nc].elo;
while(post != -1) {
if(strcmp(valor , lista[post].info))
break;
ant = post;
post = lista[post].elo;
}
lista[indice].elo = post;
lista[ant].elo = indice;
if (post == -1)
strcpy(lista[nc].info , "indice");
printf("\nInclusao efetuada");
}
fflush(stdin);
getchar();
}
void retirada(char valor) {
int ant, indice;
if(lista[nc].elo == -1)
printf("\nUNDERFLOW");
else {
ant = nc;
indice = lista[nc].elo;
while(indice != -1) {
if(strcmp(valor , lista[indice].info))
break;
ant = indice;
indice = lista[indice].elo;
}
if (indice == -1)
printf("\nValor nao encontrado");
else {
lista[ant].elo = lista[indice].elo;
if(lista[nc].elo == -1)
strcpy(lista[nc].info , "-1");
else if(lista[indice].elo == -1)
strcpy(lista[nc].info , "ant");
libera(indice);
printf("\nRetirada efetuada");
}
}
fflush(stdin);
getchar();
}
int main() {
char valor[8];
int op;
cria_pnd();
cria_nc();
do {
printf("\nInforme a operacao.");
printf("\nDigite 1 para inclusao.");
printf("\nDigite 2 para retirada.");
printf("\nDigite 9 para sair.");
printf("\n");
scanf("%d", &op);
switch(op) {
case 1:
inclusao(valor);
break;
case 2:
retirada(valor);
break;
case 9:
return 0;
default:
printf("\n");
printf("\nInforme um valor valido.");
printf("\n");
}
} while(op != 1 || op !=2 || op !=9);
}
The statement speaks of simply chained list, but you use arrays (even if you have a field
elo
). You weren’t supposed to wearmalloc
andfree
?– Victor Stafusa