Actual source code: queue.c

  1: #define PETSCKSP_DLL

  3: /**********************************queue.c*************************************

  5: Author: Henry M. Tufo III

  7: e-mail: hmt@cs.brown.edu

  9: snail-mail:
 10: Division of Applied Mathematics
 11: Brown University
 12: Providence, RI 02912

 14: Last Modification: 
 15: 6.21.97
 16: **********************************queue.c*************************************/


 19: /**********************************queue.c*************************************
 20: File Description:
 21: -----------------
 22:   This file implements the queue abstraction via a linked list ...
 23: ***********************************queue.c*************************************/
 24:  #include src/ksp/pc/impls/tfs/tfs.h

 26: /**********************************queue.c*************************************
 27: Type: queue_CDT
 28: ---------------
 29:   Basic linked list implememtation w/header node and chain.
 30: **********************************queue.c*************************************/
 31: struct node{
 32:   void  *obj;
 33:   struct node *next;
 34: };


 37: struct queue_CDT{
 38:   int len;
 39:   struct node *head, *tail;
 40: };



 44: /**********************************queue.c*************************************
 45: Function: new_queue()

 47: Input : na
 48: Output: na
 49: Return: pointer to ADT.
 50: Description: This function allocates and returns an empty queue.
 51: Usage: queue = new_queue();
 52: **********************************queue.c*************************************/
 53: queue_ADT new_queue(void)
 54: {
 55:   queue_ADT q;


 58:   q = (queue_ADT) malloc(sizeof(struct queue_CDT));
 59:   q->len = 0;
 60:   q->head = q->tail = NULL;
 61:   return(q);
 62: }



 66: /**********************************queue.c*************************************
 67: Function: free_queue()

 69: Input : pointer to ADT.
 70: Output: na
 71: Return: na
 72: Description: This function frees the storage associated with queue but not any
 73: pointer contained w/in.
 74: Usage: free_queue(queue);
 75: **********************************queue.c*************************************/
 76: void free_queue(queue_ADT q)
 77: {
 78:   struct node *hold, *rremove;

 80:   /* should use other queue fcts but what's the point */
 81:   hold = q->head;
 82:   while ((rremove = hold))
 83:     {
 84:       hold = hold->next;
 85:       free(rremove);
 86:     }

 88:   free(q);
 89: }



 93: /**********************************queue.c*************************************
 94: Function: enqueue()

 96: Input : pointer to ADT and pointer to object
 97: Output: na
 98: Return: na
 99: Description: This function adds obj to the end of the queue.
100: Usage: enqueue(queue, obj);
101: **********************************queue.c*************************************/
102: void enqueue(queue_ADT q, void *obj)
103: {
104:   if (q->len++)
105:     {q->tail= q->tail->next = (struct node *) malloc(sizeof(struct node));}
106:   else
107:     {q->tail= q->head       = (struct node *) malloc(sizeof(struct node));}

109:   q->tail->next = NULL;
110:   q->tail->obj  = obj;
111: }



115: /**********************************queue.c*************************************
116: Function: dequeue()  

118: Input : pointer to ADT
119: Output: na 
120: Return: void * to element
121: Description: This function removes the data value at the head of the queue
122: and returns it to the client.  dequeueing an empty queue is an error
123: Usage: obj = dequeue(queue);
124: **********************************queue.c*************************************/
125: void *dequeue(queue_ADT q)
126: {
127:   struct node *hold;
128:   void *obj;


131:   if (!q->len--)
132:     {error_msg_fatal("dequeue :: trying to remove from an empty queue!");}

134:   if ((hold=q->head) == q->tail)
135:     {q->head = q->tail = NULL;}
136:   else
137:     {q->head = q->head->next;}

139:   obj = hold->obj;
140:   free(hold);
141:   return(obj);
142: }



146: /**********************************queue.c*************************************
147: Function: len_queue()

149: Input : pointer to ADT
150: Output: na
151: Return: integer number of elements
152: Description: This function returns the number of elements in the queue.
153: n = len_queue(queue);
154: **********************************queue.c*************************************/
155: int len_queue(queue_ADT q)
156: {
157:   return(q->len);
158: }