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;
}
}
iwill never change.while ( i == 0 )will never be false.std::list<int>(orstd::forward_list<int>if you have a C++11 compiler).