So, I am trying to implement a simple linked list in C++, but I am having a problem with the push method of my class. Basically, when I add the first node to a list, everything goes fine; but, when I add a second node, it ends up pointing to itself (ie, secondNode.next == &secondNode).
class linkedList
{
public:
node head;
linkedList()
{
head.next = NULL;
}
void push(node new)
{
if(head.next == NULL)
{
head.next = &new;
new.next = NULL;
}
else
{
new.next = head.next;
head.next = &new;
}
}
};
I couldn't figure out what is wrong... Any help would be greatly appreciated.
newis a reserved keyword.c, theclass linkedListwouldn't be allowed -- unless you had something like#define class structit wouldn't even compile.newas a variable name in C++.