Jump to content
  • Advertisement
Sign in to follow this  
hisDudeness

double link list insertions

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am trying to get a handle on link lists (and specifically doubly linked lists) rather quickly. i have set up a quick program to begin testing my link list functions....and none of them work. ha! all i can do is start with the simplest function and go from there. this snippet simply loops through an array, and passes each index value to a newly-allocated node. for now let's pretend that my Insert() function works (which it doesn't). what i'm trying to understand is why the program craps the bed and exits without any output. any explanations would definitely point me in the right direction.
[source lang=cpp]
// Nodes for a doubly linked list.
struct node
{
	int data;
	node* next;
	node* prev;
};

// ... inside of main

// The head and tail of my list.
node* head = NULL;
node* tail = NULL;
head->data = 0;
tail->data = 0;		// Link them together.
head->prev = NULL;
head->next = tail;
tail->prev = head;
tail->next = NULL;

int i = 0;                          // Iterator
int array1[5] = { 17,20,9,83,6 };   // An array of numbers
node* temp = NULL;                  // Temp node pointer

// Loop five times and plug array indices into five nodes.
for(i = 0; i < 5; i++)
{
    temp = Allocate(array1);
                                  
    Insert(head,temp);
}

// Temp should hold the last allocated node's data, which
// should be '6'.
cout << temp->data;

// Delete the list. Pretend this works too.
DestroyList(head);

return 0;

} // End of main

// Function : Allocate
// This function takes an integer and then allocates a node pointer
// containing the integer's value as its data member.
node* Allocate(int num)
{
    node* pNode = new node;

    pNode->next = NULL;
    pNode->prev = NULL;
    pNode->data = num;

    return pNode;
}

thanks for any suggestions

Share this post


Link to post
Share on other sites
Advertisement
First off, the six lines starting with head->data = 0. Right before, you initialize both these pointers to NULL, which means they're not pointing to any place in memory, so there's no "data" member anywhere for them to access. So each of those lines is unnecessary, and will cause an error. That's all I see for now.

Ciao,
Twilight Dragon

Share this post


Link to post
Share on other sites
first off, these line should give you compilation errors:

head->data = 0;
tail->data = 0; // Link them together.
head->prev = NULL;
head->next = tail;
tail->prev = head;
tail->next = NULL;


and you are right if everything work correctly then it would output 6. I'm think the insert function might have alter the temp pointer in some way.

Share this post


Link to post
Share on other sites
@nhatkthanh: No, it won't give you compilation errors; the compiler doesn't check pointer referencing. It will give runtime errors such as he is experiencing, though.

Share this post


Link to post
Share on other sites
Okay that makes sense guys, but how do I link the head and tail pointers together? Or don't I? Do I just initialize them and connect them to the first node inserted into the list? Hmm....

Share this post


Link to post
Share on other sites
In your allocator, check if they are 0/NULL and if so, point them at the the pNode that you just created.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!