home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / autopsp.zip / LOC / LLIST.CPP < prev    next >
C/C++ Source or Header  |  1994-01-23  |  4KB  |  198 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.   if ((LL_Cur->Next) != NULL){
  118.     (*(LL_Cur->Next)).Prev=LL_Cur->Prev;
  119.   }
  120.   if ((LL_Cur->Prev) != NULL){
  121.     (*(LL_Cur->Prev)).Next=LL_Cur->Next;
  122.   }
  123.  
  124.   if (DEBUG && LL_Cur->Data != NULL)
  125.     printf("WARNING: Deleting node with non-NULL data pointer.\n");
  126.  
  127.   if (GoWhere==LL_HEAD){
  128.     LL_temp=LL_Head;
  129.   }
  130.   if (GoWhere==LL_NEXT){
  131.     LL_temp=LL_Cur->Next;
  132.   }
  133.   if (GoWhere==LL_PREV){
  134.     LL_temp=LL_Cur->Prev;
  135.   }
  136.   delete LL_Cur;
  137.   LL_Cur=LL_temp;
  138. }
  139.  
  140. void LList::Assign(void *Data)
  141. {
  142.   if (LL_Cur==NULL && DEBUG){
  143.     printf("WARNING: There has been an attempt to put data in a non-existant node.\n");
  144.     return;
  145.   }
  146.   LL_Cur->Data = Data;
  147. }
  148.  
  149. void *LList::Retrieve(int GetWhat)
  150. {
  151.   if (LL_Cur==NULL && DEBUG){
  152.     printf("WARNING: There has been an attempt to get data from a non-existant node.\n");
  153.     return(NULL);
  154.   }
  155.   if (GetWhat==LL_DATA){
  156.     return(LL_Cur->Data);
  157.   }
  158.   // This two return the values of private variables.
  159.   // They are intended only to be used in a NULL vs. NOT-NULL way.
  160.   if (GetWhat==LL_NEXT){
  161.     return(LL_Cur->Next);
  162.   }
  163.   if (GetWhat==LL_PREV){
  164.     return(LL_Cur->Prev);
  165.   }
  166.   if (GetWhat==LL_NUM_NODES){
  167.     return(&NumNodes);
  168.   }
  169.   return(0);
  170. }
  171.  
  172. void LList::Next(void)
  173. {
  174.   AtEnd=0;
  175.   if (LL_Cur->Next != NULL){
  176.     LL_Cur = LL_Cur->Next;
  177.   }
  178.   else{
  179.     AtEnd=1;
  180.   }
  181. }
  182.  
  183. void LList::Prev(void)
  184. {
  185.   AtEnd=0;
  186.   if (LL_Cur->Prev != NULL){
  187.     LL_Cur = LL_Cur->Prev;
  188.   }
  189.   else{
  190.     AtEnd=1;
  191.   }
  192. }
  193.  
  194. void LList::Restart(void)
  195. {
  196.   AtEnd=0;
  197.   LL_Cur = LL_Head;
  198. }