home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-31 | 2.5 KB | 137 lines | [TEXT/MMCC] |
- /********************************************
- **** Core Class Demo by Yves Schmid
- ****
- **** LinkedList.cp
- ****
- **** Authors: Yves Schmid
- **** Created: 14 August 1994
- **** Modified: 14 August 1994
- **** Version: 1.0
- **** Compatible: C++
- ****
- **** Description: Linked list demo
- ****
- *************************/
-
-
- #include "CoreHead.h"
-
- #include <stdio.h>
- #include <string.h>
-
-
- const long cmd_printmyself = 1024; // A command which asks the stringnode to print its string
-
-
- // A simple node which stocks a string
-
- class stringnode : public CoreNode
- {
- char data[256];
-
-
- protected:
-
-
- // Override the "receivecmd" to handle the "cmd_printmyself" command:
-
- void receivecmd(long cmd, void *info)
- {
- switch(cmd)
- {
- case cmd_printmyself: // Ok it is the right message, I can print the string.
- printf("%s\n",data);
- break;
-
- default: // Unknow message? Call the parent class
- CoreNode::receivecmd(cmd,info);
- }
- }
-
- public:
-
- // Simple constructor
-
- stringnode(CoreList *list, char *str) : CoreNode(list) // Call the parent constructor with
- // the CoreList pointer
- {
- newlevel(); // A new heritage level!
- strcpy(data,str); // Copy string
- }
- };
-
-
- void main()
- {
- CoreNode *node;
- CoreList *list;
- char str[256];
- long i;
-
- list = new CoreList; // Build a new list
-
- printf("*** CCL Linked list demo ***\n\n");
-
-
- // Ask the user to enter three strings
-
- printf("Enter a first string: ");
- scanf("%s",str);
- new stringnode(list,str);
-
- printf("Enter a second string: ");
- scanf("%s",str);
- new stringnode(list,str);
-
- printf("Enter a third string: ");
- scanf("%s",str);
- new stringnode(list,str);
-
-
- // Print the first and the last element
-
- printf("\n\n* First element of the list: ");
- list->getfirst()->docmd(cmd_printmyself);
-
- printf("* Last element of the list: ");
- list->getlast()->docmd(cmd_printmyself);
-
-
- // Scan the list
-
- printf("\n\n* Scan the list:\n");
- list->docmd(cmd_printmyself,CCF_NODES);
-
-
- // Find an element
-
- do
- {
- printf("\n\n* Enter an element number(0-2):");
- scanf("%ld",&i);
- } while(i<0 || i>2);
-
-
- // Element string
-
- printf("String is: ");
- node = list->find(i);
- node->docmd(cmd_printmyself);
-
- // Previous element
-
- printf("Previous element is: ");
- if (node->getprevious()) node->getprevious()->docmd(cmd_printmyself);
- else printf("None\n");
-
- // Next element
-
- printf("Next element is: ");
- if (node->getnext()) node->getnext()->docmd(cmd_printmyself);
- else printf("None\n");
-
- delete list; // Deletes list and elements
- }
-
-
-