home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / autopsp.zip / REGRESS / LLIST.BAK < prev    next >
Text File  |  1994-01-23  |  4KB  |  194 lines

  1. // LList Object
  2. // Linked list object definition
  3.  
  4. #include "LList.h"
  5.  
  6.  
  7. /***********  Use this main as an example **********
  8. class DumbData
  9. {
  10.   public:
  11.   char Data[10];
  12. };
  13.  
  14. void main(void)
  15. {
  16.   clrscr();
  17.   LList L;
  18.   char input[10]="XYZ";
  19.   DumbData *temp;
  20.   printf("Go!\n");
  21.  
  22.   while (0!=strlen(input)){
  23.     gets(input);
  24.     L.Add(new DumbData,LL_ADD_AFTER);
  25.     L.Next();
  26.     temp=(DumbData*)L.Retrieve(LL_DATA);
  27.     strcpy(temp->Data,input);
  28.     printf("Original value= %s\n",temp->Data);
  29.   }
  30.   while (L.Retrieve(LL_PREV) != NULL){
  31.     temp=(DumbData*)L.Retrieve(LL_DATA);
  32.     printf("%s\n",temp->Data);
  33.     L.Remove(LL_PREV);
  34. //    L.Prev();
  35.     if (kbhit()) exit(0);
  36.   }
  37.   L.Restart();
  38.   L.Assign(new DumbData);
  39.   temp=(DumbData*)L.Retrieve(LL_DATA);
  40.   strcpy(temp->Data,"This is DumbData!");
  41.   temp=(DumbData*)L.Retrieve(LL_DATA);
  42.   printf("%s\n",temp->Data);
  43. }
  44.  
  45. */
  46.  
  47.  
  48. LList::LList(void)
  49. {
  50.   LL_Head=NULL;
  51.   LL_Cur=NULL;
  52.   NumNodes=0;
  53.   AtEnd=0;
  54. }
  55.  
  56. LList::~LList(void)
  57. {
  58.   LL_Cur=LL_Head;
  59.   if (LL_Head != NULL){
  60.     while (LL_Cur->Next != NULL){
  61.       Next();
  62.     }
  63.     while (LL_Cur->Prev != NULL){
  64.       Remove(LL_PREV);
  65.     }
  66.     Remove(LL_HEAD);
  67.   }
  68.   if (NumNodes) printf("WARNING: Lost nodes\n");
  69. }
  70.  
  71. void LList::Add(void *Data, int BorF)
  72. {
  73.   LLNode *noclue;
  74.  
  75.   LL_temp=new LLNode;
  76.   ++NumNodes;
  77.   LL_temp->Data = Data;
  78.  
  79.   if(LL_Cur != NULL){
  80.     if (BorF==LL_ADD_AFTER){
  81.       LL_temp->Next = LL_Cur->Next;
  82.       LL_Cur->Next = LL_temp;
  83.       LL_temp->Prev = LL_Cur;
  84.  
  85.       noclue = LL_temp->Next;
  86.       if (noclue != NULL) {
  87.     (*noclue).Prev=LL_temp;
  88.       }
  89.     }
  90.     if (BorF==LL_ADD_BEFORE){
  91.       LL_temp->Prev = LL_Cur->Prev;
  92.       LL_Cur->Prev = LL_temp;
  93.       LL_temp->Next = LL_Cur;
  94.  
  95.       noclue=LL_temp->Prev;
  96.       if (noclue != NULL){
  97.     (*noclue).Next=LL_temp;
  98.       }
  99.     }
  100.   }
  101.   else{
  102.     LL_temp->Next=NULL;
  103.     LL_temp->Prev=NULL;
  104.     LL_Cur = LL_temp;
  105.     LL_Head = LL_temp;
  106.   }
  107. }
  108.  
  109. void LList::Remove(int GoWhere)
  110. {
  111.   if (LL_Cur==NULL) return;
  112.   --NumNodes;
  113.  
  114.   delete(LL_Cur->Data); // Need to confirm that this is deleting OK.
  115.   LL_Cur->Data=NULL;    //   Sure would be cool if it was.
  116.  
  117.   (*(LL_Cur->Next)).Prev=LL_Cur->Prev;
  118.   (*(LL_Cur->Prev)).Next=LL_Cur->Next;
  119.  
  120.   if (DEBUG && LL_Cur->Data != NULL)
  121.     printf("WARNING: Deleting node with non-NULL data pointer.\n");
  122.  
  123.   if (GoWhere==LL_HEAD){
  124.     LL_temp=LL_Head;
  125.   }
  126.   if (GoWhere==LL_NEXT){
  127.     LL_temp=LL_Cur->Next;
  128.   }
  129.   if (GoWhere==LL_PREV){
  130.     LL_temp=LL_Cur->Prev;
  131.   }
  132.   delete LL_Cur;
  133.   LL_Cur=LL_temp;
  134. }
  135.  
  136. void LList::Assign(void *Data)
  137. {
  138.   if (LL_Cur==NULL && DEBUG){
  139.     printf("WARNING: There has been an attempt to put data in a non-existant node.\n");
  140.     return;
  141.   }
  142.   LL_Cur->Data = Data;
  143. }
  144.  
  145. void *LList::Retrieve(int GetWhat)
  146. {
  147.   if (LL_Cur==NULL && DEBUG){
  148.     printf("WARNING: There has been an attempt to get data from a non-existant node.\n");
  149.     return(NULL);
  150.   }
  151.   if (GetWhat==LL_DATA){
  152.     return(LL_Cur->Data);
  153.   }
  154.   // This two return the values of private variables.
  155.   // They are intended only to be used in a NULL vs. NOT-NULL way.
  156.   if (GetWhat==LL_NEXT){
  157.     return(LL_Cur->Next);
  158.   }
  159.   if (GetWhat==LL_PREV){
  160.     return(LL_Cur->Prev);
  161.   }
  162.   if (GetWhat==LL_NUM_NODES){
  163.     return(&NumNodes);
  164.   }
  165.   return(0);
  166. }
  167.  
  168. void LList::Next(void)
  169. {
  170.   AtEnd=0;
  171.   if (LL_Cur->Next != NULL){
  172.     LL_Cur = LL_Cur->Next;
  173.   }
  174.   else{
  175.     AtEnd=1;
  176.   }
  177. }
  178.  
  179. void LList::Prev(void)
  180. {
  181.   AtEnd=0;
  182.   if (LL_Cur->Prev != NULL){
  183.     LL_Cur = LL_Cur->Prev;
  184.   }
  185.   else{
  186.     AtEnd=1;
  187.   }
  188. }
  189.  
  190. void LList::Restart(void)
  191. {
  192.   AtEnd=0;
  193.   LL_Cur = LL_Head;
  194. }