Displaying a Double-chained Circular List

2

How do I display values entered in a Double-chained Circular List?

Follow my code with my attempt:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <malloc.h>
#include <conio.h>

using namespace std;

int x, num;

struct lista
{
    int valor;
    struct lista * prox;
    struct lista * ant;
};

struct lista * inicio;
struct lista * fim;
struct lista * aux;

//Insere na lista o número passado por parãmetro
void insere(int valor)
{
    aux = (struct lista *)malloc(sizeof(struct lista));
    aux->valor = valor;
    aux->prox = NULL;
    aux->ant = NULL;

    if (inicio == NULL)
    {
        inicio = aux;
    }
    else
    {
        fim->prox = aux;
        aux->ant = fim;
    }
    fim = aux;
    fim->prox;
    inicio->ant = fim;
}

//Mostra todos os elementos da lista
void exibirElementos()
{ 
    while (1)
    {
        cout << inicio->valor;
        inicio->prox;
        if (inicio->prox->valor == fim->valor)
            break;
    }
}

int main()
{
    inicio = NULL;
    fim = NULL;
    aux = NULL;
}

The method that is responsible for displaying the values is exibirElementos() .

I am using Visual Studio 2013 to compile.

    
asked by anonymous 18.11.2014 / 13:10

1 answer

1

First, place the signature of method exibirElementos1 shortly after the declaration of struct lista
void exibirElementos1(struct lista * node);

Do a recursive function that will go through your entire list as follows.

    void exibirElementos()
    {
        exibirElementos1(&inicio);
    }

    void exibirElementos1(lista *node)
    {
        if (node != null)
        {
            cout<<node->valor
            exibirElementos1(&node->prox);
        }
    }

This way it starts with the first node in your list, having you have a node declared with the name inicio , and for exactly the last node in your list, printing all on the screen.

And iteratively

    void exibirElementosIterativo(lista* node)
    {
        while (node != NULL)
        {
            cout << node->valor;
            node = node->prox;
        }
    }
    
18.11.2014 / 13:17