🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Linked lists

Started by
2 comments, last by Mr_Black 24 years, 7 months ago
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?

Advertisement
Thanks a lot much appreciated
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.
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

This topic is closed to new replies.

Advertisement