How to print a dynamic list in c++?

Asked

Viewed 34 times

0

Good evening! is my first post here so please in case I reported something wrong, feel free to fix it. I made a dynamic char list, but I’m having difficulty printing the entire list on the screen, when trying to print, it prints only the first character and then stops compiling stating that some of the other characters point to Nullptr. Below I will put the . h of the Node class ,my function and the main.

The Nodo class:

#include <iostream>
using namespace std;
class Nodo
{
protected:
    char caractere;
    Nodo* proximo;
    Nodo* anterior;
public:
    Nodo();
    ~Nodo();
    void setcaractere(char c);
    void setProximo(Nodo* p);
    Nodo* getAnterior();
    char getcaractere();
    Nodo* getProximo();
    void setAnterior(Nodo* anter);
};

Print function:

{
    Nodo* ptr8 = new Nodo;
    Nodo* ptr9 = new Nodo;
    ptr9 = inicio;
    for (int i = 0; i < tamanho; i++) 
    {
        cout << ptr9->getcaractere() << endl;
        ptr8 = inicio->getProximo();
        ptr9 = ptr8;
    }
}

My main:

#include "Nodo.h"
#include"Lista.h"
#include <iostream>
using namespace std;
int main()
{
    Lista lista;
    lista.push_front('a');
    lista.push_back('b');
    lista.push_back('c');
    lista.push_front('d');
    lista.imprimir();
    lista.pop_back();
    lista.pop_front();
    if (!lista.empty())
    {
        cout << lista.front() << endl;
        cout << lista.back() << endl;
    }
    else {
        cout << "pilha vazia" << endl;
    }
    lista.remove();
    lista.salvarTXT();
    lista.back();
}

1 answer

-1


I couldn’t understand what you tried to do or even ask.

In the code below it seems that you are using the list methods provided by the language...

 {
    Lista lista;
    lista.push_front('a');
    lista.push_back('b');
    lista.push_back('c');
    lista.push_front('d');
    lista.imprimir();
    lista.pop_back();
    lista.pop_front();
    if (!lista.empty())
    {
        cout << lista.front() << endl;
        cout << lista.back() << endl;
    }
    else {
        cout << "pilha vazia" << endl;
    }
    lista.remove();
    lista.salvarTXT();
    lista.back();

Either you implemented methods with the same name for your class and did not post?

And where’s the rest of the code?

And about that part

class Nodo
{
protected:
    char caractere;
    Nodo* proximo;
    Nodo* anterior;
public:
    Nodo();
    ~Nodo();
    void setcaractere(char c);
    void setProximo(Nodo* p);
    Nodo* getAnterior();
    char getcaractere();
    Nodo* getProximo();
    void setAnterior(Nodo* anter);
};

What is the idea of implementing this and at the same time the other functions that showed? They seem to be the same thing...

  • Understand that a list is a container. The list has nodes, and each node in general has a reference to a given.
  • a list is not a node.
  • a node is not a list.
  • a knot is not a die.

If you want to use the list as offered in #include understand that Lá is an iterable class and then you can use an iterator to scroll through the list, or even one for displaying the values. The common is to create a class for your data and put the list inside. And reset the << operator to show the list.

If that’s what you want to do, use the list as provided, reply and write an example.

If you want to use this in your list implementation just implement some methods such as Begin() end() and operators * != and ++ I think.

  • Thanks! Yes, I had created the pop, top and push function, but I didn’t put it because they were huge

  • I understand. But without the compileable code, it’s hard to help. And these functions being immense is something to think about, because in general it has few lines, about 10/15.

Browser other questions tagged

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