Chained Allocation - Data Structure - C

Asked

Viewed 196 times

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:

  1. Inclusion of GREEN information
  2. Inclusion of RED information
  3. Inclusion of ORANGE information
  4. Withdrawal of GREEN information
  5. Inclusion of OCRE information
  6. Inclusion of ROSA information
  7. Removal of first information in logical order
  8. Inclusion of BLACK information
  9. Inclusion of GREY information
  10. Withdrawal of information RED
  11. Withdrawal of information BLACK
  12. Removal of the last information in logical order
  13. Inclusion of WHITE information
  14. Inclusion of LILAC information
  15. Withdrawal of information GRAY
  16. 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);
}
  • 1

    The statement speaks of simply chained list, but you use arrays (even if you have a field elo). You weren’t supposed to wear malloc and free?

1 answer

0


void retirada(char valor[]) //precisa ser uma string

void inclusao(char valor[]) //precisa ser um string

Change char value per char valor[]

this creates a parameter capable of receiving a string.

If you do not put it this way the strcpy and strcpm functions will not work. They need to receive a string.

See below the code without compilation errors.

Now study him and see if the chained list is OK

#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[]) //precisa ser um string
{
    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[]) //precisa ser uma string
{
    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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.