• Advertisement

Archived

This topic is now archived and is closed to further replies.

Linked lists

This topic is 6637 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

Ah, Hell, I've got a spare ten minutes while my code compiles.
Okay, basic structure for a linked list

typedef struct
{
int i;
myStruct *next; // Pointer to the next node in the linked list
myStruct *previous; // Pointer to the previous node in the linked list
}myStruct;

myStruct *head; // Pointer to the head of the linked list
myStruct *tail; // Pointer to the tail of the linked list

void
GenerateAndAddNewNode(void)
{
myStruct *newNode = new myStruct; // Generate a new node dynamically
if(!head) // If there is no current list
{
head = tail = newNode; // The head and tail of the list now point at the new node
head->previous = NULL; // Which points at nothing
head->next = NULL;
}
else
{
tail->next = newNode; // The tail now points on to the new node
newNode->previous = tail; // Which points back to the tail
newNode->next = NULL; // And on to nothing
tail = newNode; // The tail is advanced to the last node
}
}

Removing a node involves setting up the next and previous pointers of the nodes (if they exist) before and after the node to be deleted then deleting the node with the delete function

Does that help?

Share this post


Link to post
Share on other sites
Advertisement
Ok I know this is a basic subject, but I can remember for the life of me how to do them. Does anyone know how to do them or could you point me to an article on them.

Share this post


Link to post
Share on other sites
Here's an easier way to do it.

#include "list"
using namespace std;

list mystruct_linkedlist;
list::iterator mystruct_linkedlist_iter;

And from there, to add a mystruct to the end,

mystruct_linkedlist.push_back(your_var);

The front,

mystruct_linkedlist.push_front(your_var);

And use the iterators to delete and traverse the list

Share this post


Link to post
Share on other sites

  • Advertisement