How to display a Double Chained Circular List

Asked

Viewed 1,383 times

2

How do I display the 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’m using the Visual Studio 2013 to compile.

  • I can post a pseudo-code?

  • @Enzotiezzi Can yes.

1 answer

1


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

Make a recursive function that will pass 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 on your list, having you have a node declared with the name inicio, and to exactly the last node of your list printing everyone on screen.

And iteratively

    void exibirElementosIterativo(lista* node)
    {
        while (node != NULL)
        {
            cout << node->valor;
            node = node->prox;
        }
    }
  • This way you are giving error, is saying that the method does not need arguments. 'exibirElementos()':function does not take 1 arguments

  • update the code so I can see how you did it

  • I edited there, changed the name of the second method exibirElementos for exibirElementos1.

  • Updated question

  • still presenting the same problem ? edited again, give a look now

  • Now you’re giving another problem: 'exibirElementos1': identifier not found. Not finding the function within the method exibirElementos()

Show 2 more comments

Browser other questions tagged

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