trying to create a basic linked list
for some reason I'm having trouble making a basic linked list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
using namespace std;
struct node
{
int data;
node * next;
};
class List1
{
public:
node * head;
node * curr;
int length;
List1()
{
head = 0;
curr = head;
}
void createNode()
{
curr = new node;
curr = curr->next;
length++;
}
void printNodes()
{
node * traveler;
traveler = head;
while (traveler != curr->next)
{
cout << traveler << '\n';
traveler = traveler->next;
}
}
};
|
trying to access head->next after creating at least two nodes keeps giving me errors. not sure why.
Just follow your state mutations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
(enter List1::List1())
head = ?
curr = ?
(head = 0;)
head = nullptr
curr = ?
(curr = head;)
head = nullptr
curr = nullptr
(leave List1::List1())
(enter List1::createNode())
head = nullptr
curr = nullptr
(curr = new node)
head = nullptr
curr->data = ?
curr->next = ?
(curr = curr->next)
head = nullptr
curr = ?
(leave List1::createNode())
(enter List1::printNodes())
head = nullptr
curr = ?
traveler = ?
(traveler = head)
head = nullptr
curr = ?
traveler = nullptr
(while (traveler != curr->next))
(Undefined behavior is triggered at this point. You attempted to dereference
curr, which has an undefined value.)
|
Last edited on
Topic archived. No new replies allowed.