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);
}
The E pointer means what ? and why you called the inverters inside itself again ?
– Lucas Alves
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.
– Lucas Trigueiro
What would ant be? You made double chained list?
– Maurício Z.B