0

Okay I know this is a ridiculously easy question, but for some reason I cannot get a linked list to work. It may just be because I am really tired, because I've done them a million times before. Boiled my program down to the simplest possible implementation, still not working.

Very basic implementation, just make a LL of integers, something I have done a million times before but for whatever reason it's never progressing past head.

main.cpp

#include <iostream>
#include "ll.h"
using namespace std;

int main()
{
    int x;
    list ll;
    int i =0;


    while(i == 0)
    {
    cout << "Enter a value to add to the LL ";
    cin >> x;

    ll.add(x);
    ll.display();
    }

return 0;
}

ll.h

struct node
{
    int val;
    node * next;
};

class list
{
    public:
    list();

    void add(int);
    void display();
    node * head;
};

ll.cpp

#include <iostream>
#include "ll.h"
using namespace std;

list::list()
{
    head = NULL;
}

void list::add(int x)
{
    if(!head)
    {
        cout << "First  " << endl;
        head = new node;
        head->val = x;
        head->next = NULL;
    }
    else
    {
        node * current = head;
        while (current)
            current = current->next;

        current = new node;
        current->val = x;
        current->next = NULL;

    }
}

void list::display()
{
    node * current = head;

    while(current)
    {
        cout << current->val << endl;
        current = current->next;
    }
}
3
  • 1
    The while loop in your main won't work because the value of i will never change. while ( i == 0 ) will never be false. Commented Nov 11, 2012 at 12:30
  • 1
    What is not working? Describe the undesired behavior you are getting Commented Nov 11, 2012 at 12:30
  • Of course, in real code you should be using std::list<int> (or std::forward_list<int> if you have a C++11 compiler). Commented Nov 11, 2012 at 12:36

2 Answers 2

2

It seems you want to append to the list. In this case, you loop condition shouldn't be

while (current)

but

while (current->next)

making sure is initially non-NULL (which you do with your check for the `head).

Actually, the logic for setting up the new node is also not quite right. You probably want to have the second branch of add() look something like this:

while (current->next) {
    current = current->next;
}
current->next = new node(x);

... with a suitable constructor of node:

node::node(int x): val(x), next() {}
Sign up to request clarification or add additional context in comments.

3 Comments

And do current->next = new Node when you are done instead of instantly overriding current with a new value
I agree, it is just an addition to finalizing the answer. I totally agree and have upvoted the answer.
Yeah I figured it out right after I posted the question. That's what I get for trying to code at 4 am.
1

In addition to Dietmar's answer, you have an improper while loop:

while ( i == 0 ) {
     ...
}

In the body of the for loop, i is never changed, causing it to loop indefinitely. I'm not entirely sure what you want to use i for though.

1 Comment

It is just an infinite loop, adding elements as long as you keep typing ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.