Chained Allocation - Data Structure - C

1

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:

  • Inclusion of GREEN information
  • Adding information RED
  • Inclusion of information ORANGE
  • Removal of GREEN information
  • Inclusion of OCRE information
  • Inclusion of ROSA information
  • Retrieving the first information in logical order
  • Inclusion of information BLACK
  • Inclusion of information GRAY
  • Withdrawal of information RED
  • Withdrawal of the information BLACK
  • Withdrawal of the last information in logical order
  • Inclusion of WHITE information
  • Inclusion of LILÁS information
  • Withdrawal of information GRAY
  • Withdrawal of WHITE information
  • 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);
    }
    
        
    asked by anonymous 05.09.2017 / 00:39

    1 answer

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

    Change% of% value by% with%

    This creates a parameter capable of receiving a string.

    If you do not put strcpy and strcpm in this way, they will not work. They need to get a string.

    See the code below without any compilation errors.

    Now study it and see if the linked 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);
    }
    
        
    05.09.2017 / 15:25