home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / Animation Class Library / CCL / Examples / a.linkedlists / LinkedList.cp next >
Encoding:
Text File  |  1994-08-31  |  2.5 KB  |  137 lines  |  [TEXT/MMCC]

  1. /********************************************
  2.  **** Core Class Demo by Yves Schmid
  3.  ****
  4.  **** LinkedList.cp
  5.  ****
  6.  **** Authors:        Yves Schmid  
  7.  **** Created:      14 August 1994
  8.  **** Modified:     14 August 1994
  9.  **** Version:      1.0
  10.  **** Compatible:   C++
  11.  ****
  12.  **** Description:  Linked list demo
  13.  ****
  14.  *************************/
  15.  
  16.  
  17. #include "CoreHead.h"
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22.  
  23. const long cmd_printmyself    = 1024;    // A command which asks the stringnode to print its string
  24.  
  25.  
  26. // A simple node which stocks a string
  27.  
  28. class stringnode : public CoreNode
  29. {
  30.     char    data[256];
  31.  
  32.  
  33.     protected:
  34.  
  35.  
  36.     // Override the "receivecmd" to handle the "cmd_printmyself" command:
  37.  
  38.     void receivecmd(long cmd, void *info)
  39.     {
  40.         switch(cmd)
  41.         {
  42.             case cmd_printmyself:    // Ok it is the right message, I can print the string.
  43.             printf("%s\n",data);    
  44.             break;
  45.         
  46.             default:                // Unknow message? Call the parent class
  47.             CoreNode::receivecmd(cmd,info);
  48.         }
  49.     }
  50.  
  51.     public:
  52.  
  53.     // Simple constructor
  54.  
  55.     stringnode(CoreList *list, char *str) : CoreNode(list)    // Call the parent constructor with
  56.                                                             // the CoreList pointer 
  57.     {
  58.         newlevel();            // A new heritage level!
  59.         strcpy(data,str);    // Copy string
  60.     }
  61. };
  62.  
  63.  
  64. void main()
  65. {
  66.     CoreNode    *node;
  67.     CoreList    *list;
  68.     char        str[256];
  69.     long        i;
  70.  
  71.     list = new CoreList;    // Build a new list
  72.  
  73.     printf("*** CCL Linked list demo ***\n\n");
  74.  
  75.  
  76.     // Ask the user to enter three strings
  77.  
  78.     printf("Enter a first string: ");
  79.     scanf("%s",str);
  80.     new stringnode(list,str);
  81.     
  82.     printf("Enter a second string: ");
  83.     scanf("%s",str);
  84.     new stringnode(list,str);
  85.  
  86.     printf("Enter a third string: ");
  87.     scanf("%s",str);
  88.     new stringnode(list,str);
  89.  
  90.  
  91.     // Print the first and the last element
  92.  
  93.     printf("\n\n* First element of the list: ");
  94.     list->getfirst()->docmd(cmd_printmyself);
  95.     
  96.     printf("* Last element of the list: ");
  97.     list->getlast()->docmd(cmd_printmyself);
  98.  
  99.  
  100.     // Scan the list
  101.  
  102.     printf("\n\n* Scan the list:\n");
  103.     list->docmd(cmd_printmyself,CCF_NODES);
  104.  
  105.  
  106.     // Find an element
  107.  
  108.     do
  109.     {
  110.         printf("\n\n* Enter an element number(0-2):");
  111.         scanf("%ld",&i);
  112.     } while(i<0 || i>2);
  113.     
  114.     
  115.     // Element string
  116.     
  117.     printf("String is: ");    
  118.     node = list->find(i);
  119.     node->docmd(cmd_printmyself);
  120.  
  121.     // Previous element
  122.  
  123.     printf("Previous element is: ");
  124.     if (node->getprevious()) node->getprevious()->docmd(cmd_printmyself);
  125.     else printf("None\n");
  126.  
  127.     // Next element
  128.  
  129.     printf("Next element is: ");
  130.     if (node->getnext()) node->getnext()->docmd(cmd_printmyself);
  131.     else printf("None\n");
  132.     
  133.     delete list;    // Deletes list and elements
  134. }
  135.  
  136.  
  137.