TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: John Grey
from: Kurt Kuzba
date: 1998-07-03 05:24:50
subject: LISTLINK.C 3/5

/*_|_|   LISTLINK.C   ( part 3 of 5 )
_|_|_|   Heavily commented example of using linked lists in C.
_|_|_|   No warrantee or guarantee is given or implied.
_|_|_|   PUBLIC DOMAIN   by Kurt Kuzba.  (7/3/1998)*/
void Del_pList(LinkedList *LL, void *p)
{                              /*   Remove unknown list element.  */
   int i;                      /*   Create an int to count with.  */
   ListData *LD = LL->item;    /*   Set up a ListData pointer.    */
   LinkedList *LI = LL;        /*   Set pointer to list pointer.  */
   for(i = 1; i items; i++)
   {                           /*   Walk the list looking for the */
      LI = LI->next;           /*   list item equal to pointer    */
      if(LI->item == p)        /*   given as an function argument.*/
      {
         Del_iList(LL, i);     /*   If and when a matching item   */
         break;                /*   is found, remove it and exit. */
      }
   }
}
void Erase_List(LinkedList *LL)
{                              /*   Remove all list elements.     */
   while(Count_List(LL))       /*   While the list has elements.  */
      Del_iList(LL, 1);        /*   Remove the first element.     */
}
void Kill_List(LinkedList *LL)
{                              /*   Remove all traces of a list.  */
   Erase_List(LL);             /*   Remove all list elements.     */
   free(LL->item);             /*   Free the ListData pointer.    */
   free(LL);                   /*   Free the LinkedList pointer.  */
}
void Add_To_List(LinkedList *LL, void *p, int i)
{                              /*   Add an element to the list.   */
   LinkedList *nxt, *prv, *added = malloc(sizeof(LinkedList));
   ListData *LD = LL->item;    /*   Set up a ListData pointer.    */
   added->item = p;            /*   Assign item to new element.   */
   if(0 == LD->items)          /*   Test for only list element.   */
   {                           /*   Link as first and last.       */
      LL->next = LL->prev = added;
      added->prev = added->next = NULL;
      LD->items++;             /*   Update list counter.          */
      return;
   }
   if(i > LD->items || i < 1)  /*   Test for out-of-list element. */
   {
      prv = LL->prev;          /*   Link as last element and      */
      prv->next = LL->prev = added;
      added->prev = prv;       /*   link to prev last element.    */
      added->next = NULL;      /*   Set next link to NULL.        */
      LD->items++;             /*   Update list counter.          */
      return;
   }                           /*   It must be a midlist element. */
   nxt = List_Walk(LL, i);     /*   Find present element at i.    */
   prv = nxt->prev;            /*   Get the prev link at i.       */
   added->next = nxt;          /*   Link to next element.         */
   added->prev = prv;          /*   Link to prev element.         */
   prv->next = nxt->prev = added; /*Link next and prev to element.*/
   LD->items++;                /*   Update list counter.          */
   Set_It(LL, LD->iterator);   /*   Update list iterator.         */
}
/*_|_|   end   LISTLINK.C   ( part 3 of 5 )   */

> ] By tomorrow, this will all seem like a dream................

---
* Origin: *YOPS ]I[* 8.4 GIG * RA/FD/FE * Milwaukee, WI (1:154/750)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728
SEEN-BY: 639/252 670/213 218
@PATH: 154/750 222 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.