0

I'm trying to implement my own linked list using vectors and pointers. The problem I'm have is that I can't get the first node to point to the second node.

Here's my code and what I've tried:

struct Node {
    Node* previous;
    Node* next;

    int data;
};

// Initialize: Create Vector size 20 and first node
void LinkedList::init() {
    vecList.resize(20, NULL); // Vector of size 20
    Node* head = new Node();  // Create head node
    head->previous = NULL;    // Previous point set to null
    head->next = vecList[1];  // Next pointer set to next position
    head->data = 0;           // Data set at value 0

    vecList[0] = head; // Put head node in first position
    count = 1; // Increase count by 1
}

// Add Node to array
void LinkedList::push_back(Node* node, int data) {
    count += 1;
    node = new Node();
    node->next = vecList[count + 1];
    node->previous = vecList[count - 1];
    node->data = data;
    vecList[count - 1] = node;
}

The data has been passed in and will displayed using:

cout << linkedlist.vecList[1]->data << endl;

But if I try this way to display I get error saying the next pointer is <Unable to read memory>

cout << linkedlist.vecList[0]->next->data << endl;
6
  • How are you calling LinkedList::push_back? Commented Oct 11, 2014 at 13:41
  • 2
    What is LinkedList? What is vecList? How do you use the code? What error do you get? Commented Oct 11, 2014 at 13:42
  • And if the errors are not build errors, then have you tried to step through the code line by line in a debugger? Commented Oct 11, 2014 at 13:43
  • LinkedList is the class and vecList is the vector of nodes. The error i get is "Access violation reading location 0x00000008." Went through the debugger and form what i can see the problem is my pointers are not pointing to any thing Commented Oct 11, 2014 at 13:47
  • @ilent2 this is how i call it : linkedlist.push_back(node, 64); it adds the data but pointers to next and previous point to notting Commented Oct 11, 2014 at 13:49

2 Answers 2

2

You forgot to set the next pointer of the previous Node in the push_back method. If count is a member variable of the list containing the number of entries you have to change the method like this:

EDIT: actually you have to increment count in the end because array indices start with zero.

void LinkedList::push_back(Node * node, int data){  
    node = new Node();
    node->next = NULL;  // NULL because next element does not exist yet
    node->previous = vecList[count - 1];
    node->data = data;
    vecList[count] = node;
    vecList[count-1]->next = vecList[count];
    count++;
}

Still it's a bit strange that you try to implement a linked list with a vector or array because that actually defeats the advantages of a list...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sorted my problem :) and as for why i'm using a vector to store the list ya can ask my lecturer
2

It looks like vecList is a vector/array of pointers to Node.

When you init, you let the first element point to the second element:

void LinkedList::init(){
    ...
    head->next = vecList[1]; 

But at this point, the second element does not exist yet. So you cannot point to it yet. Something similar is wrong at the push_back function.

Comments

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.