Re: [newbie] linked lists

From: Sammy (Samedi@cris.com)
Date: 01/14/97


On Tue, 14 Jan 1997, Cyber Reaper wrote:

- ok... I feel that I have reached the point where I will need linked lists...
- but I dont know how to make them... can anyone give me a quick rundown on
- makeing linked list.. it would be much apreataded<sp?> 

They're very easy.  It's just a bunch of structures that point to each
other:

struct character_list {
  struct char;
  struct character_list *next;
}

This is a simple linked list element that will hold an bunch of characters
(like people in a room, people on the mud, etc).

/* global declaration of the pointer to the list*/
struct character_list *top_of_list;

/* build a linked list with a few mobs in it */
void build_dummy_list(void) {
  struct character_list *list_pointer;

/* create the first entry */
  CREATE(top_of_list, struct character_list, 1);

/* create a mob and put it in the list */
  top_of_list->char = read_mobile(1); /* mobile with an rnum of 1 */

/* make the next list element and put another mob in it */

  CREATE(list_pointer, struct character_list, 1);
  list_pointer->char = read_mobile(2); /* I have no idea which mobs these are */

/* add the new element on to the list */

  top_of_list->next = list_pointer;

/* now top_of_list->next->char = mob 2 */
}

That's the basics of it.  All you're doing is creating memory for elements
in the list, and pointing the ->next pointer to the new element.  You can
have more data in the structure, and you can also link backwards if you
add a struct character_list *previous to the character_list structure.
For examples, you could search the code for descriptor_list, which is a
linked list of all descriptors (sockets) connected to the game.

Sam

+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST