How do I get this list double-chained and circulated?

Asked

Viewed 1,505 times

2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Tipo_Lista{
    char cod[50] ;
    struct Tipo_Lista *Prox;
    struct Tipo_Lista *Ant;
};
struct Tipo_Lista *Primeiro;
struct Tipo_Lista *Ultimo;

void FLVazia(){
    struct Tipo_Lista *aux;
    aux = (struct Tipo_Lista*)malloc(sizeof(struct Tipo_Lista));
    Primeiro = aux;
    Ultimo = Primeiro;
    Primeiro->Ant = NULL;

}
void Insere(char *x){
    struct Tipo_Lista *aux;
    aux = (struct Tipo_Lista*)malloc(sizeof(struct Tipo_Lista));
    strcpy(aux->cod,x);
    Ultimo->Prox = aux;
    aux->Ant = Ultimo;
    Ultimo = Ultimo->Prox;
    aux->Prox = NULL;
}
void Imprime(){
    struct Tipo_Lista *aux;
    aux = Primeiro->Prox;
    while(aux != NULL){
        printf("Item = %s\n",aux->cod);
        aux = aux->Prox;
    }
}
void Imprime_Atras(){
    struct Tipo_Lista *aux;
    aux = Ultimo;
    while(aux->Ant != NULL){
        printf("Item = %s\n",aux->cod);
        aux = aux->Ant;
    }
}
void Pesquisa(char *x){
    struct Tipo_Lista *aux;
    struct Tipo_Lista *aux2;

    int flag = 0;
    aux = Primeiro->Prox;
    while(aux != NULL){



        if(strcmp (aux->cod,x) == 0){
            aux2 = aux->Ant;
            aux = aux->Prox;
            printf("Achou item %s seu anterior é %s seu próximo é %s ", x,aux2->cod,aux->cod);
            flag = 1;
            aux = NULL;
        }
        else 
        aux = aux->Prox;
    }
    if(flag == 0){
        printf("Item %s Não se encontra nesta Lista!!!!",x);
    }
}

void Remove(char *x){
    int retorno;
    struct Tipo_Lista *aux;
    int flag = 0;
    aux = Primeiro->Prox;
    while(aux != NULL){
        retorno = strcmp (aux->cod,x);
        if(retorno == 0){
            if(aux->Prox == NULL){
                Ultimo = aux->Ant;
                aux->Ant->Prox = NULL;
                aux = NULL;
                flag = 1;
            }
            else{

            printf("Removeu item %s\n",x);
            aux->Ant->Prox = aux->Prox;
            aux->Prox->Ant = aux->Ant;
            aux = NULL;
            flag = 1;
        }
        }
        else
        aux = aux->Prox;
    }
    free(aux);
    if (flag == 0){
        printf("Item %s Não se encontra nesta Lista!!",x);
    }
}


int main(int argc, char *argv[]) {
    char op = '2';
    int i;
    char codigo[50];
    FLVazia();
    while(op != '0'){
        printf("Encontre com o codigo: ");
        gets(codigo);
        Insere(codigo);
        printf("\n\n\nContinuar com o cadastro? 1 = sim 0 = Não: \n\n");
        op = getch();
    }
    Imprime();
    printf("\n\n Entre com um item para pesquisa: ");
    gets(codigo);
    Pesquisa(codigo);
    printf("Item a ser removido: ");
    gets(codigo);
    Remove(codigo);
    Imprime();
    getch();
    Imprime_Atras();
    getch();
        return 0;
}
  • Could you give more details about what you want? What part of the code you would like to change or improve?

  • this all double chained , I would like to circulate it now , so that when I called the function search , passing the last element it returned me the penultimate as previous and the first as next , And if the first were to pass that he would return to me the last as the former and the second as the next.

  • For a list to be circular the last element must point the next to the first element of the list, whereas the first element of the list must point its previous to the last of the list. My suggestion is to modify the "Inserts" method it should have the role of controlling the circular list, will also need to modify the Remove method as it should be smart enough for cases of deletion of the last element that causes modification of the reference pointers.

1 answer

1

Next the last element has to point to the list head and the list head to the last element,you can grab the list head pointer by taking the field next to the penultimate element of the list.

Browser other questions tagged

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