Home > Mobile >  Adding first into a doubly linked list and also traversing though the linked list and outputting the
Adding first into a doubly linked list and also traversing though the linked list and outputting the

Time:11-25

I was trying to add each data first into my doubly linked list and then display the data but it's not giving me the correct output.

#include <iostream>
using namespace std;

class node {
public:
    string name;
    string title;
    node* next;
    node* prev;
};
class doublylinkedlist {
public:
    node* head = NULL;
    void addfirst(string name, string title) {
        node* p = new node();
        p->name = name;
        p->title = title;
        p->prev = NULL;
        p->next = head;
        if (head != NULL)
            head->prev = p;

        head = p;
    }
    void print() {
        node* w = head;
        if (head = NULL)
            cout << "the linked list is an empty one" << endl;
        else {
            while (w != NULL)
            cout << "our artist name is that " << w->name << endl;
            cout << "our song title is that" << w->title << endl;
            w = w->next;
        }
    }
};

int main()
{
    std::cout << "Hello World!\n";
    doublylinkedlist dll;
    dll.addfirst("Henok", "flower");
    dll.addfirst("terrence", "now");
    dll.addfirst("walter", "dog");
    dll.print();

}

I expected to add first the data and get an out of "Walter dog, Terrence now and Henok flower" but the out am get is not correct what is the issue with my code

CodePudding user response:

The problem was not in your "addfirst" function, but in you "print" function.

You mixed up "=" with "==" and you forgot brackets around the statements of your while loop.

Please use the below corrected print "function":

    void print() {
        node* w = head;
        if (head == NULL)
            cout << "the linked list is an empty one" << endl;
        else {
            while (w != NULL) {
                cout << "our artist name is that " << w->name << endl;
                cout << "our song title is that" << w->title << endl;
                w = w->next;
            }
        }
    }

CodePudding user response:

Why are you creating your own container? If this is for a C class you should be learning how to use the containers in the standard library. I've included code that provides a solution using the std::list. I also replace the std::endl with '\n'; The std::endl generates the \n but also flushes the stream, which is not needed in this case.

#include <iostream>
#include <list>

struct node {
  std::string name_;
  std::string title_;
};

int main(int argc, char* argv[]) {
  std::list<node> dllist;
  dllist.push_front(node{"Henok", "flower"});
  dllist.push_front(node{"terrence", "now"});
  dllist.push_front(node{"walter", "dog"});
  if (dllist.empty()) {
    std::cout << "The list is empty\n";
  } else {
    for (auto& n : dllist) {
      std::cout << "our artist name is that " << n.name_ << '\n';
      std::cout << "our song title is that " << n.title_ << '\n';
    }
  }
}
  • Related