home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d913 / stickit.lha / StickIt / Source / Source.lha / linkedli.c < prev    next >
C/C++ Source or Header  |  1993-06-01  |  3KB  |  130 lines

  1. /*********************************************
  2.  ***************  linkedlist.c  **************
  3.  *********************************************/
  4.  
  5. #include <exec/types.h>
  6. #include <graphics/text.h>
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #include "consts.h"
  12. #include "structs.h"
  13. #include "prototype.h"
  14.  
  15. /***   lle == linked list element   ***/
  16.  
  17. struct ndnode *llealloc(void)
  18.    {
  19.    struct ndnode *temp;
  20.  
  21.    temp = (struct ndnode *)malloc(sizeof(struct ndnode));
  22.  
  23.    if (temp == NULL)
  24.       error ("Linked list error",ERR_MALLOC);
  25.  
  26.    temp->data = NULL;
  27.  
  28.    return (temp);
  29.    }
  30.  
  31. /* Add new node after here */
  32.  
  33. void lleadd(struct ndnode *here,struct ndnode *new)
  34.    {
  35.    new->next = here->next;
  36.    new->prev = here;
  37.    here->next = new;
  38.    new->next->prev = new;
  39.    }
  40.  
  41. void llfree(struct ndnode *here)
  42.    {
  43.    struct ndnode *temp;
  44.  
  45.    do
  46.       {
  47.       temp=here->next;
  48.  
  49.       /* If there's any data connected to the node, free it first */
  50.  
  51.       if (here->data)
  52.          free((void *)here->data);
  53.  
  54.       free((void *)here);
  55.  
  56.       here=temp;
  57.       } while (here!=NULL);
  58.    }
  59.  
  60. /***   Frees from here to end, not freeing tail node   ***/
  61.  
  62. void llfreemid(struct ndnode *here)
  63.    {
  64.    struct ndnode *temp,*store;
  65.  
  66.    if ((here->next == NULL) || (here->prev == NULL))    /* If at head or tail */
  67.       return;
  68.  
  69.    store = here->prev;
  70.  
  71.    do
  72.       {
  73.       temp=here->next;
  74.  
  75.       /* If there's any data connected to the node, free it first */
  76.  
  77.       if (here->data)
  78.          free((void *)here->data);
  79.  
  80.       free((void *)here);
  81.  
  82.       here=temp;
  83.       } while (here->next != NULL);
  84.  
  85.    /***   Patch links   ***/
  86.  
  87.    store->next = here;
  88.    here->prev = store;
  89.    }
  90.  
  91. void lleremove(struct ndnode *here)
  92.    {
  93.    here->prev->next = here->next;
  94.    here->next->prev = here->prev;
  95.  
  96.    if (here->data != NULL)
  97.       free((void *)here->data);
  98.  
  99.    free((void *)here);
  100.    }
  101.  
  102. void llstart(struct ndnode *start,struct ndnode *end)
  103.    {
  104.    start->prev =
  105.       end->next = NULL;
  106.  
  107.    start->data =
  108.       end->data = NULL;
  109.  
  110.    start->next = end;
  111.    end->prev = start;
  112.    }
  113.  
  114. /***   StickIt routines *******************************************************/
  115.  
  116. extern prjptr prj;
  117.  
  118. /******************************************************************************
  119. Function : void initlists(void)
  120. Purpose : Initialises the linked lists.
  121. *******************************************************************************/
  122.  
  123. void initlists()
  124.     {
  125.     prj->notes_start = llealloc();
  126.     prj->notes_end = llealloc();
  127.  
  128.     llstart(prj->notes_start,prj->notes_end);
  129.     }
  130.