How can I invert a simple chained list without using a previous pointer

Asked

Viewed 6,045 times

1

I have to solve a data structure fiction problem that consists of reversing a chained list but how can I invert a list if I don’t have its elements (Less of course p-start and p->end )follows the code

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

typedef struct TipoElemento {
    int valor;
    struct TipoElemento *proximo;
}TElemento;


typedef struct TipoLista {
    TElemento *inicio;
    TElemento *fim;
}TLista;

void inicializar (TLista *p){
    p->inicio = NULL;
    p->fim = NULL;
}

void inserir (TLista *p){
    TElemento *novo;
    novo = new TElemento;
    printf("Entre com um novo elemento ");
    scanf("%d",&novo->valor);
    novo->proximo = NULL;

    if (p->inicio == NULL){
        p->inicio = novo;
        p->fim = novo;
    }else{
        p->fim->proximo = novo;
        p->fim = novo;

    }

}

void apresentar (TLista *p){
    TElemento *aux = p->inicio;          // ao criar a variavel aux devemos inicializar com o primeiro elemento da LISTA // P->INICIO

    if(p->inicio == NULL){
        printf("Lista vazia ");
    }else{
        while(aux != NULL){  // eu quero APRESENTAR MINHA LISTA ----AUX
            printf("%d",aux->valor);
            aux = aux->proximo;
        }
    }
}

void inverter (TLista *p){
    TLista *aux = p->fim;

    where (){
        aux = 
    }

}

int main (){
    TLista aep;
    TLista aep2;
    int opcao;

    inicializar(&aep);
    inicializar (&aep2);
    do 
    {
        printf("\n ****** ESTRUTURAS DE DADOS - PILHA ESTATICA ******\n");
        printf("\nOpcoes: \n\n");
        printf(" 1 - Inserir novo elemento \n");
    //  printf(" 2 - Consultar elemento \n");
    //  printf(" 3 - Remover elemento \n");
        printf(" 4 - Apresentar todos os elementos \n");
    //  printf(" 0 - para sair \n\n");
    //  printf("Entre com a sua opcao: ");
        scanf("%d", &opcao); /* Le a opcao do usuario */
        switch (opcao)
        {
            case 1: inserir(&aep); break;
            //case 2: consultar(&E); break;
        //  case 3: retirarPilha(&P); break;
            case 4: apresentar(&aep); break;
            case 0: break;
            default: printf("\n\n Opcao invalida"); getch(); break;
        }
    } while (opcao != 0);
}

2 answers

2


The first function reverses the list elements and the second function reverses the start and end reference.

void inverteElementos (TElemento *e, TElemento *ant){
    if(e->proximo!=NULL)
        inverteElementos(e->proximo, e);
    e->proximo = ant;
}

void inverter (TLista *p){
    inverteElementos(p->inicio, NULL);

    //Inverte inicio com fim
    TElemento *aux = p->inicio;
    p->inicio = p->fim;
    p->fim = aux;
}
  • The E pointer means what ? and why you called the inverters inside itself again ?

  • The E pointer is only one element from the list that will be passed to the function (starting with the first element). The invertebrates calling itself, is called recursion, look for in case you have not worked with recursive functions yet. Basically what the inverted function does in this case is to go through the entire list by always passing the address of the previous one, then it goes back to the list making each element point to its previous element.

  • What would ant be? You made double chained list?

-1

void inverter_lista(struct no **lista)
{
  struct no* nova_lista = NULL;
  struct no* tmp;
  int i;

  
    while (*lista != NULL)
    {
        tmp = *lista;
        *lista = (*lista)->proximo;
      tmp->proximo = nova_lista;
      nova_lista = tmp;
    }
  *lista = nova_lista;

}

Browser other questions tagged

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