Actual source code: queue.c

  1: /*$Id: queue.c,v 1.2 2001/04/10 19:37:38 bsmith Exp $*/
  2: /**********************************queue.c*************************************
  3: SPARSE GATHER-SCATTER PACKAGE: bss_malloc bss_malloc ivec error comm gs queue

  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 <stdio.h>
 25:  #include queue.h
 26:  #include error.h
 27: #include "bss_malloc.h"


 30: /**********************************queue.c*************************************
 31: Type: queue_CDT
 32: ---------------
 33:   Basic linked list implememtation w/header node and chain.
 34: **********************************queue.c*************************************/
 35: struct node{
 36:   void  *obj;
 37:   struct node *next;
 38: };


 41: struct queue_CDT{
 42:   int len;
 43:   struct node *head, *tail;
 44: };



 48: /**********************************queue.c*************************************
 49: Function: new_queue()

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


 62:   q = (queue_ADT) bss_malloc(sizeof(struct queue_CDT));
 63:   q->len = 0;
 64:   q->head = q->tail = NULL;
 65:   return(q);
 66: }



 70: /**********************************queue.c*************************************
 71: Function: free_queue()

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

 84: #ifdef DEBUG
 85:   if (!q->len)
 86:     {
 87:       if (q->head || q->tail)
 88:         {error_msg_fatal("free_queue :: len=0 but head, tail not NULL?");}
 89:     }

 91:   if (q->len)
 92:     {
 93:       if (!q->head || !q->tail)
 94:         {error_msg_fatal("free_queue :: len!=0 but head, tail are NULL?");}
 95:     }
 96: #endif

 98: #ifdef INFO
 99:   /* could be destroying the only pointer to prev malloced space!!! */
100:   if (q->len)
101:     {error_msg_warning("free_queue :: prev malloced space?");}
102: #endif

104:   /* should use other queue fcts but what's the point */
105:   hold = q->head;
106:   while ((rremove = hold))
107:     {
108:       hold = hold->next;
109:       bss_free(rremove);
110:     }

112:   bss_free(q);
113: }



117: /**********************************queue.c*************************************
118: Function: enqueue()

120: Input : pointer to ADT and pointer to object
121: Output: na
122: Return: na
123: Description: This function adds obj to the end of the queue.
124: Usage: enqueue(queue, obj);
125: **********************************queue.c*************************************/
126: void enqueue(queue_ADT q, void *obj)
127: {
128:   if (q->len++)
129:     {q->tail= q->tail->next = (struct node *) bss_malloc(sizeof(struct node));}
130:   else
131:     {q->tail= q->head       = (struct node *) bss_malloc(sizeof(struct node));}

133:   q->tail->next = NULL;
134:   q->tail->obj  = obj;
135: }



139: /**********************************queue.c*************************************
140: Function: dequeue()  

142: Input : pointer to ADT
143: Output: na 
144: Return: void * to element
145: Description: This function removes the data value at the head of the queue
146: and returns it to the client.  dequeueing an empty queue is an error
147: Usage: obj = dequeue(queue);
148: **********************************queue.c*************************************/
149: void *dequeue(queue_ADT q)
150: {
151:   struct node *hold;
152:   void *obj;


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

158:   if ((hold=q->head) == q->tail)
159:     {q->head = q->tail = NULL;}
160:   else
161:     {q->head = q->head->next;}

163: #ifdef DEBUG
164:   if (!hold)
165:     {error_msg_fatal("dequeue :: len > 0 but head NULL?");}
166: #endif

168:   obj = hold->obj;
169:   bss_free(hold);
170:   return(obj);
171: }



175: /**********************************queue.c*************************************
176: Function: len_queue()

178: Input : pointer to ADT
179: Output: na
180: Return: integer number of elements
181: Description: This function returns the number of elements in the queue.
182: n = len_queue(queue);
183: **********************************queue.c*************************************/
184: int len_queue(queue_ADT q)
185: {
186:   return(q->len);
187: }