home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1989 / 03 / extra / queue.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-22  |  3.2 KB  |  155 lines

  1. /*----------------------------------------------*/
  2. /*                  QUEUE.C                     */
  3. /*         Modul zum Turbo C Maustreiber        */
  4. /*          (C) 1988 R.Ziegaus & TOOLBOX        */
  5. /*----------------------------------------------*/
  6.  
  7. /*
  8. Im Modul Queue ist eine typenunabhängige Schlange
  9. verwirklicht worden, d. h. daß die Schlange nicht
  10. auf irgendeinen Datentyp beschränkt ist, sondern
  11. beliebig ist. Außerdem ist es möglich, mehrere
  12. Schlangen gleichzeitig zu bearbeiten. Eine
  13. Anwendung findet sich in EVENTS.C
  14. */
  15.  
  16.  
  17. #include <alloc.h>
  18. #include <mem.h>
  19. #include "queue.h"
  20.  
  21. #define MALLOC(x) ((x *) malloc(sizeof(x)))
  22.  
  23.  
  24. static Node *InitNode(void *val, Node *Next,
  25.                       int Size)
  26.  
  27. /*
  28. InitNode allocates memory for a new stack entry
  29. and initializes it with the values of the parameters
  30. */
  31.  
  32. {
  33.   Node *new;
  34.   void *temp;
  35.  
  36.   /* Allocate space for new entry */
  37.   new = (Node *) malloc(sizeof(Node));
  38.   /* Allocate space for new Item  */
  39.   temp = (void *) malloc(Size);
  40.   /* Copy Item                    */
  41.   memcpy(temp, val, Size);
  42.   /* init values                  */
  43.   if (new != NULL && temp != NULL )
  44.     {
  45.       new->Item = temp;
  46.       new->Next = Next;
  47.     }
  48.     /* Return address of new entry  */
  49.   return(new);
  50. }
  51.  
  52.  
  53. QueueHead *InitQueue(int Size)
  54.  
  55. /* InitQueue sets up a new QueueHead */
  56.  
  57. {
  58.   QueueHead   *new;
  59.  
  60.   /*  Allocate memory for QueueHead  head */
  61.   new =  MALLOC(QueueHead);
  62.   /*  Memory allocation ok ? */
  63.   if (new != NULL)
  64.     {
  65.       /* Init QueueHead  head */
  66.       new->Len    = 0;
  67.       new->Head  =  NULL;
  68.       new->Tail   = NULL;
  69.       /* Size of QueueHead items */
  70.       new->Size   = Size;
  71.     }
  72.   /* Return address of QueueHead */
  73.   return(new);
  74. }
  75.  
  76.  
  77. int  AppendToQueue(void *data, QueueHead *Queue)
  78.  
  79. /* AppendToQueue appends a new Item to the queue */
  80.  
  81. {
  82.   Node *new;
  83.  
  84.   /* Create new node */
  85.   new = InitNode(data, NULL, Queue->Size);
  86.   if (new != NULL)
  87.     {
  88.       if (Queue->Len)
  89.     /* replace Head Item with the new entry */
  90.     Queue->Tail->Next = new;
  91.       else
  92.   Queue->Head = new;
  93.       Queue->Tail = new;
  94.       /* increase QueueHead length */
  95.       Queue->Len++;
  96.       return(TRUE);
  97.     }
  98.   else
  99.     return(FALSE);
  100. }
  101.  
  102.  
  103. void *GetQueueHead(QueueHead *Queue)
  104.  
  105. /* GetQueueHead proves the first element (the head)
  106.    of the queue                                  */
  107.  
  108. {
  109.   Node   *temp;
  110.   void   *data;
  111.   int    Size;
  112.  
  113.   Size = Queue->Size;
  114.   if (Queue->Len > 0)/* if the queue is not empty */
  115.     {
  116.       temp = Queue->Head; /* get Head of queue */
  117.       data = (void *) malloc(Size);
  118.       movmem(temp->Item, data, Size);
  119.       Queue->Head = temp->Next;
  120.       Queue->Len--;       /* adjust queue length */
  121.       if (Queue->Len == 0)
  122.   Queue->Tail = NULL;
  123.       /* release space of the Item */
  124.       free((void *) temp->Item);
  125.       /* release space for one entry */
  126.       free((void *) temp);
  127.       return(data);
  128.     }
  129.   else
  130.     return(NULL);
  131. }
  132.  
  133.  
  134. int QueueLength (QueueHead *Queue)
  135.  
  136. {
  137.   return(Queue->Len);
  138. }
  139.  
  140.  
  141. int EmptyQueue (QueueHead *Queue)
  142.  
  143. {
  144.   return(Queue->Len == 0);
  145. }
  146.  
  147.  
  148. void KillQueue (QueueHead *Queue)
  149.  
  150. /* KillQueue deletes head of QueueHead */
  151.  
  152. {
  153.   free(Queue);
  154. }
  155.