home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / LINKLIST.ZIP / LINKLIST.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-11  |  5.7 KB  |  279 lines

  1. ///////////////////////////////////////////////////////
  2. //                                                   //
  3. //   Linked List Class Functions - Version 1.00      //
  4. //                                                   //
  5. //           By Kevin Campbell 22/7/95               //
  6. //                                                   //
  7. //                                                   //
  8. ///////////////////////////////////////////////////////
  9.  
  10. struct far details{
  11.   char forename[20];
  12.   char surname[20];
  13.   char day;
  14.   char month;
  15.   int year;
  16. };
  17.  
  18. struct far linkinfo{
  19.   void *data;
  20.   int size;
  21.   struct far linkinfo *link;
  22.   struct far linkinfo *oldlink;
  23. };
  24.  
  25. template <class DX>
  26. class linked_list{
  27.   struct linkinfo *curr; // only single entry required for linked list
  28.   int entries;
  29.   int curr_entry;
  30. public:
  31.   linked_list(void);          // Constructor
  32.   ~linked_list(void);         // DeConstructor
  33.   char add();             // Adds new entry with 'size' data allocated
  34.   char kill(void);            // Removes entry from list and frees up memory
  35.   char get(void *info);        // Gets data from entry
  36.   char put(void *info);        // Puts data to entry
  37.   char last();             // Uses last entry in list
  38.   char next();             // Uses next entry in list
  39.   char rewind();              // Goes to start of list
  40.   char fastforward();         // Goes to end of list
  41.   int num();             // Returns number of entries in list
  42.   DX *addr();            // Returns pointer to data
  43. };
  44.  
  45. // *Note - all char entries return 1 on successful
  46. //         completion and 0 on failure.
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. //
  54. //
  55. // *Note - templated functions must be inside include file
  56. //
  57. //
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. ///////////////////////////////////////////////////////
  65. //                                                   //
  66. //   Linked List Class Functions - Version 1.00      //
  67. //                                                   //
  68. //           By Kevin Campbell 22/7/95               //
  69. //                                                   //
  70. //                                                   //
  71. ///////////////////////////////////////////////////////
  72.  
  73. #include <string.h>
  74. #include <stdlib.h>
  75. #include <stdio.h>
  76. #include <conio.h>
  77. #include <alloc.h>
  78.  
  79. template <class DX>
  80. char linked_list<DX>::add(){
  81.   char far *tempstore;
  82.   DX *tempdata;
  83.   struct far linkinfo *nextlink;
  84.   struct far linkinfo *templink;
  85.  
  86.   if (tempdata=new DX){//[ammount]){
  87.     if (templink=(linkinfo *)malloc(sizeof(linkinfo))){
  88.       if (curr){ // curr exists and should be linked from
  89.  
  90.     // Preserve Structure
  91.  
  92.     nextlink=curr->link;
  93.  
  94.     templink->link=curr->link;
  95.     templink->oldlink=curr;
  96.  
  97.     nextlink->oldlink=templink;
  98.     curr->link=templink;
  99.  
  100.     templink->data=tempdata;
  101.  
  102.     //templink->size=ammount;
  103.  
  104.     curr=templink; // Set current Linked-List position to new struct
  105.  
  106.     curr_entry++;
  107.  
  108.     ///////////////////////////////////
  109.  
  110.       }else{
  111.     // Linked List does not exist
  112.     // Create new linked list structure
  113.     templink->link=NULL;
  114.     templink->oldlink=NULL;
  115.     templink->data=tempdata;
  116.     //templink->size=ammount;
  117.     curr=templink;
  118.     curr_entry++;
  119.       }
  120.       entries++;
  121.     }else{
  122.       delete tempdata;
  123.       return(0);
  124.     }
  125.     return(1);
  126.   }else{
  127.     // Allocation of tempdata failed
  128.     return(0);
  129.   }
  130. }
  131.  
  132. template <class DX>
  133. char linked_list<DX>::kill(void){
  134.   struct far linkinfo *templink;
  135.   struct far linkinfo *nextlink;
  136.   struct far linkinfo *oldlink;
  137.  
  138.   if(!(curr==NULL)){ // if curr exists
  139.  
  140.     // Detach and Rebuild Structure
  141.  
  142.     nextlink=curr->link;
  143.     oldlink=curr->oldlink;
  144.  
  145.     if(oldlink)oldlink->link=nextlink;
  146.     if(nextlink)nextlink->oldlink=oldlink;
  147.  
  148.     // Entry is now completely removed from structure
  149.  
  150.     templink=curr; // Store removed entry address for deallocation
  151.  
  152.     ///////////////////////////////////
  153.  
  154.     if ((curr->link==NULL)&(curr->oldlink==NULL)){
  155.       curr=NULL;
  156.       curr_entry=0;
  157.     }else{
  158.       if(!(curr->oldlink==NULL)){
  159.     curr=curr->oldlink;
  160.     curr_entry--;
  161.       }else{
  162.     curr=curr->link;
  163.       }
  164.     }
  165.  
  166.     delete (DX *)templink->data;
  167.     free(templink); // Deallocate memory and return to heap
  168.     entries--;
  169.  
  170.     return(1);
  171.  
  172.   }else{
  173.     return(0);
  174.   }
  175. }
  176.  
  177. template <class DX>
  178. char linked_list<DX>::get(void *info){
  179.   if(!(curr==NULL)){
  180.     if(memcpy(info,curr->data,sizeof(DX))){
  181.       return(1);
  182.     }else{
  183.       return(0);
  184.     }
  185.   }else{
  186.     //Linked list does not exist, return error
  187.     return(0);
  188.   }
  189. }
  190.  
  191. template <class DX>
  192. char linked_list<DX>::put(void *info){
  193.   if(!(curr==NULL)){
  194.     if(memcpy(curr->data,info,sizeof(DX))){
  195.       return(1);
  196.     }else{
  197.       return(0);
  198.     }
  199.   }else{
  200.     //Linked list does not exist, return error
  201.     return(0);
  202.   }
  203. }
  204.  
  205. template <class DX>
  206. char linked_list<DX>::last(){
  207.   if(curr){
  208.     if(curr->oldlink){
  209.       curr=curr->oldlink;
  210.       return(1);
  211.     }else{
  212.       return(0);
  213.     }
  214.   }else{
  215.     return(0);
  216.   }
  217. }
  218.  
  219. template <class DX>
  220. char linked_list<DX>::next(){
  221.   if(curr){
  222.     if(curr->link){
  223.       curr=curr->link;
  224.       return(1);
  225.     }else{
  226.       return(0);
  227.     }
  228.   }else{
  229.     return(0);
  230.   }
  231. }
  232.  
  233. template <class DX>
  234. char linked_list<DX>::rewind(){
  235.   if(curr){
  236.     while (last());
  237.     return(1);
  238.   }else{
  239.     return(0);
  240.   }
  241. }
  242.  
  243. template <class DX>
  244. char linked_list<DX>::fastforward(){
  245.   if(curr){
  246.     while (next());
  247.     return(1);
  248.   }else{
  249.     return(0);
  250.   }
  251. }
  252.  
  253. template <class DX>
  254. int linked_list<DX>::num(){
  255.   return(entries);
  256. }
  257.  
  258. template <class DX>
  259. DX *linked_list<DX>::addr(){
  260.   if(curr){
  261.     return((DX *)curr->data);
  262.   }else{
  263.     return(NULL);
  264.   }
  265. }
  266.  
  267. template <class DX>
  268. linked_list<DX>::linked_list(void){
  269.   entries=0;
  270.   curr=NULL;
  271.   curr_entry=0;
  272. }
  273.  
  274. template <class DX>
  275. linked_list<DX>::~linked_list(void){
  276.   while(kill());
  277. }
  278.  
  279.