/*
Author: aveon QQ: 1243128
Date: 05-10-04 21: 18
Description: Generic queue
*/
# Ifndef avalon_queue_h
# Define avalon_queue_h
# Ifndef ava_bool
# Define ava_bool
# Define true 1
# Define false 0
Typedef int bool;
# Endif
Typedef struct link * lhandle;
Extern lhandle initqueue (size_t type );
/* Construct */
Extern bool queueempty (lhandle Q );
/* Empty? */
Extern int queuelength (lhandle Q );
/* Length */
Extern bool gethead (lhandle Q, void * ELEM );
/* If the queue is not empty, copy the Header element and return true */
Extern bool enqueue (lhandle Q, void * ELEM );
/* Insert the element ELEM as the new team-End Element */
Extern bool dequeue (lhandle Q, void * ELEM );
/* If ELEM is not empty, assign the deleted header node value to ELEM and return true */
Extern bool clearqueue (lhandle Q );
/* Clear */
Extern bool destroyqueue (lhandle * q );
/* Destroy */
# Endif
//////////////////////
///////////////////
/*
Author: aveon QQ: 1243128
Date: 05-10-04 21: 18
Description: Generic queue
*/
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
# Ifndef ava_bool
# Define ava_bool
# Define true 1
# Define false 0
Typedef int bool;
# Endif
Typedef struct node {
Void * data;/* Data Pointer */
Struct node * Next;/* next node */
} Node, * nhandle;
Typedef struct link {
Nhandle front;/* head pointer */
Nhandle rear;/* tail pointer */
Int size;/* length */
Size_t type;
} Link, * lhandle;
/*
Internal functions
*/
Static nhandle allocnode (lhandle S, void * ELEM)
{/* Distribution node */
Nhandle node_temp = (nhandle) malloc (sizeof (node ));
Void * data_temp;
Assert (null! = Node_temp );
Assert (null! = S );
Assert (null! = ELEM );
Data_temp = malloc (S-> type );
Assert (null! = Data_temp );
Memcpy (data_temp, ELEM, S-> type);/* Copy Data */
Node_temp-> DATA = data_temp;/* member data value assignment */
Return node_temp;
}
Static bool freenode (nhandle * node)
{/* Release node */
Free (* node)-> data );
Free (* node );
Return true;
}
/*
External Functions
*/
Extern lhandle initqueue (size_t type)
{/* Constructor */
Lhandle temp = (lhandle) malloc (sizeof (Link ));
Assert (null! = Temp );
Temp-> front = temp-> rear = NULL;
Temp-> size = 0;
Temp-> type = type;
Return temp;
}
Extern bool queueempty (lhandle q)
{/* Empty? */
Assert (null! = Q );
Return (0 = Q-> size )? True: false;
}
Extern int queuelength (lhandle q)
{/* Length */
Assert (null! = Q );
Return Q-> size;
}
Extern bool gethead (lhandle Q, void * ELEM)
{/* If the queue is not empty, copy the Header element and return true */
Assert (null! = Q );
Assert (null! = ELEM );
If (null = Q-> front) return false;
Memcpy (ELEM, Q-> front-> data, Q-> type );
Return true;
}
Extern bool enqueue (lhandle Q, void * ELEM)
{/* Insert the element ELEM as the new team-End Element */
Nhandle temp;
Assert (null! = Q );
Assert (null! = ELEM );
Temp = allocnode (Q, ELEM );
/**/
If (Q-> size) ++! = 0) {/* length plus 1 */
Q-> rear-> next = temp;
Q-> rear = Q-> rear-> next;
}
Else {
Q-> front = Q-> rear = temp;
}
Return true;
}
Extern bool dequeue (lhandle Q, void * ELEM)
{/* If ELEM is not empty, assign the deleted header node value to ELEM and return true */
Nhandle temp;
Assert (null! = Q );
If (0 = Q-> size) return false;
Temp = Q-> front-> next;/* New team head */
If (null! = ELEM)/* Copy to ELEM */
Memcpy (ELEM, Q-> front, Q-> type );
Freenode (& (Q-> front ));
If (Q-> size )--! = 1 ){
Q-> front = temp;
}
Else {
Q-> front = Q-> rear = NULL;
}
Return true;
}
Extern bool clearqueue (lhandle q)
{/* Clear */
Assert (null! = Q );
While (0! = Q-> size)
Dequeue (Q, null );
Return true;
}
Extern bool destroyqueue (lhandle * q)
{/* Destroy */
Assert (Q );
Assert (* q );
Clearqueue (* q );
Free (* q );
Return true;
}