# Include <iostream> Using namespace STD;Typedef int elemtype; Typedef struct Node { Elemtype data; Struct node * next; } Node, * nodeptr; Typedef struct { Nodeptr front;/* head and tail pointer */ Nodeptr rear; } Linkqueue; /* Construct an empty queue */ Bool initqueue (linkqueue * LP) { Cout <"init queue..." <Endl; Nodeptr P = (nodeptr) malloc (sizeof (node )); P-> next = NULL; Lp-> front = Lp-> rear = P; Return true; } /* Destroy the queue, including the header node */ Bool destroyqueue (linkqueue * LP) { Cout <"Destroy queue..." <Endl; While (LP-> front) { Lp-> rear = Lp-> front-> next; Free (LP-> front ); Lp-> front = Lp-> rear; } Return true; } /* Empty queue, reserved header node */ Bool clearqueue (linkqueue * LP) { Cout <"Clear queue..." <Endl; Nodeptr P = Lp-> front-> next; Lp-> front-> next = NULL; Lp-> rear = Lp-> front; Nodeptr Q; While (P) { Q = p-> next; Free (P ); P = Q; } Return true; } Bool queueempty (linkqueue SCSI) { Return SCSI. Front = SCSI. rear; } Int queuelength (linkqueue SCSI) { Int I = 0; If (SCSI. Front = NULL) Return 0; Nodeptr P = SCSI. Front-> next; While (P) { ++ I; P = p-> next; } Return I; } Bool gethead (linkqueue SCSI, elemtype * PE) { Nodeptr P; If (SCSI. Front = SCSI. rear) Return false; P = SCSI. Front-> next; * Pe = p-> data; Cout <"get head item:" <* PE <Endl; Return true; } /* Insert the element ELEM as the new team-end element of the queue */ Bool enqueue (linkqueue * LP, elemtype ELEM) { Cout <"enqueue item" <ELEM <Endl; Nodeptr S = (nodeptr) malloc (sizeof (node )); S-> DATA = ELEM; S-> next = NULL; Lp-> rear-> next = s; Lp-> rear = s; Return true; } /* Delete the queue Header element and use * PE to return its value */ Bool dequeue (linkqueue * LP, elemtype * PE) { If (LP-> front = Lp-> rear) Return false; Nodeptr P = Lp-> front-> next; * Pe = p-> data; Cout <"dequeue item" <* PE <Endl; Lp-> front-> next = p-> next; If (LP-> rear = P)/* If the team header is the team end, delete the rear and point it to the header node */ Lp-> rear = Lp-> front; Free (P ); Return true; } /* Output each element in the queue from the beginning to the end */ Bool queuetraverse (linkqueue SCSI) { Cout <"queue traverse..." <Endl; Nodeptr P = SCSI. Front-> next; While (P) { Cout <p-> data <''; P = p-> next; } Cout <Endl; Return true; } Int main (void) { Linkqueue SCSI; Initqueue (& SCSI ); For (INT I = 0; I <5; I ++) Enqueue (& SCSI, I ); Queuetraverse (SCSI ); Int result; Gethead (SCSI, & result ); Dequeue (& SCSI, & result ); Queuetraverse (SCSI ); If (! Queueempty (SCSI )) Cout <"Queue Length:" <queuelength (SCSI) <Endl; /* Clearqueue (& SCSI );*/ Destroyqueue (& SCSI ); Cout <"Queue Length:" <queuelength (SCSI) <Endl; Return 0; } |