Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Linked lists


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Mr_Black   Members   -  Reputation: 131

Posted 19 November 1999 - 04:15 AM

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.

Sponsor:

#2 MikeD   Members   -  Reputation: 158

Posted 19 November 1999 - 01:22 AM

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?


#3 Mr_Black   Members   -  Reputation: 131

Posted 19 November 1999 - 02:01 AM

Thanks a lot much appreciated

#4 Zer   Members   -  Reputation: 122

Posted 19 November 1999 - 04:15 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS