Consider a storage area of 5 nodes. On this area will be mounted a list simply chained with increasing order by the field INFO, of name LIST. Initially create the PND and NODO-HEAD to make this list operational. After, show the behavior of the list during the following operations:
My code does not give any compilation errors, but the result is not as expected. can anybody help me? Here's 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);
}