home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377a.lha / libraries / exec / list_queue / lists.c next >
Encoding:
C/C++ Source or Header  |  1980-02-04  |  3.9 KB  |  147 lines

  1. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  2.  *
  3.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  4.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals.
  5.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  6.  * information on the correct usage of the techniques and operating system
  7.  * functions presented in this example.  The source and executable code of
  8.  * this example may only be distributed in free electronic form, via bulletin
  9.  * board or as part of a fully non-commercial and freely redistributable
  10.  * diskette.  Both the source and executable code (including comments) must
  11.  * be included, without modification, in any copy.  This example may not be
  12.  * published in printed form or distributed with any commercial product.
  13.  * However, the programming techniques and support routines set forth in
  14.  * this example may be used in the development of original executable
  15.  * software products for Commodore Amiga computers.
  16.  * All other rights reserved.
  17.  * This example is provided "as-is" and is subject to change; no warranties
  18.  * are made.  All use is at your own risk.  No liability or responsibility
  19.  * is assumed.
  20.  */
  21.  
  22. /*
  23.  * Lists.Example - Example program for the lists chapter
  24.  */
  25.  
  26. #include <exec/types.h>
  27. #include <exec/memory.h>
  28. #include <exec/lists.h>
  29. #include <exec/nodes.h>
  30.  
  31. #include <proto/all.h>
  32.  
  33. #include <string.h>
  34. #include <stdio.h>
  35.  
  36. int CXBRK(VOID) { return(0); }
  37.  
  38. struct MyNodes {
  39.     struct Node My_Node;
  40.     UBYTE   data[64];
  41. };
  42.  
  43. /* The type of our name_nodes... */
  44. #define NAME_NODE   27
  45.  
  46. VOID AddName(struct List *list,UBYTE *name)
  47. {
  48. struct MyNodes *name_node;
  49.  
  50.     if (name_node=AllocMem(sizeof(struct MyNodes),MEMF_PUBLIC|MEMF_CLEAR))
  51.     {
  52.         strcpy(name_node->data,name);
  53.         name_node->My_Node.ln_Name=name_node->data;
  54.         name_node->My_Node.ln_Type=NAME_NODE;
  55.         name_node->My_Node.ln_Pri=0;
  56.         AddHead(list,(struct Node *)name_node);
  57.     }
  58. }
  59.  
  60. /*
  61.  * This function frees the whole list, including the header.
  62.  * Note that it does not update the header as it is freeing the list.
  63.  * It is also very important that you do not reference memory
  64.  * that has been freed.
  65.  */
  66. VOID FreeMyList(struct List *list)
  67. {
  68. struct MyNodes *name_node;
  69. struct MyNodes *next_node;
  70.  
  71.     name_node=(struct MyNodes *)(list->lh_Head);
  72.     while (next_node=(struct MyNodes *)(name_node->My_Node.ln_Succ))
  73.     {
  74.         FreeMem(name_node,sizeof(struct MyNodes));
  75.         name_node=next_node;
  76.     }
  77.     FreeMem(list,sizeof(struct List));
  78. }
  79.  
  80. VOID DisplayList(struct List *list)
  81. {
  82. struct Node *node;
  83.  
  84.     if (list->lh_TailPred==(struct Node *)list)
  85.     {
  86.         printf("List is empty.\n");
  87.     }
  88.     else
  89.     {
  90.         for (node=list->lh_Head;node->ln_Succ;node=node->ln_Succ)
  91.         {
  92.             printf("%lx -> %s\n",node,node->ln_Name);
  93.         }
  94.     }
  95. }
  96.  
  97. VOID DisplayName(struct List *list,UBYTE *name)
  98. {
  99. struct Node *node;
  100.  
  101.     if (node=FindName(list,name))
  102.     {
  103.         while (node)
  104.         {
  105.             printf("Found %s at node %lx\n",node->ln_Name,node);
  106.             node=FindName((struct List *)node,name);
  107.         }
  108.     }
  109.     else printf("No node with %s found.\n",name);
  110. }
  111.  
  112. VOID main(int argc, UBYTE *argv[])
  113. {
  114. struct  List    *MyList;
  115.  
  116.     if (MyList=AllocMem(sizeof(struct List),MEMF_PUBLIC|MEMF_CLEAR))
  117.     {
  118.         NewList(MyList);
  119.  
  120.         AddName(MyList,"Name7");
  121.         AddName(MyList,"Name6");
  122.         AddName(MyList,"Name5");
  123.         AddName(MyList,"Name4");
  124.         AddName(MyList,"Name3");
  125.         AddName(MyList,"Name2");
  126.         AddName(MyList,"Name1");
  127.         AddName(MyList,"Name0");
  128.  
  129.         AddName(MyList,"Name7");
  130.         AddName(MyList,"Name6");
  131.         AddName(MyList,"Name5");
  132.         AddName(MyList,"Name4");
  133.         AddName(MyList,"Name3");
  134.         AddName(MyList,"Name2");
  135.         AddName(MyList,"Name1");
  136.         AddName(MyList,"Name0");
  137.  
  138.         if (argc)
  139.         {
  140.             DisplayName(MyList,"Name5");
  141.             DisplayList(MyList);
  142.         }
  143.  
  144.         FreeMyList(MyList);
  145.     }
  146. }
  147.