Data Structure --- chain queue in C Language
// Chain queue storage // Yang Xin # include
# Include
Typedef int QElemType; // defines the node typedef struct QNode {QElemType data; struct QNode * next;} QNode, * QueuePtr; // defines the pointer typedef struct {QueuePtr front; QueuePtr rear ;} linkQueue; // insert element e into the queue void en_Queue (LinkQueue * q, QElemType e) {QueuePtr temp = (QueuePtr) malloc (sizeof (QNode); if (temp) {temp-> data = e; temp-> next = NULL; q-> rear-> next = temp; q-> rear = temp ;}} // initialize the queue void init_Queue (LinkQueue * q) {q-> front = q-> rear = (QNode *) malloc (sizeof (QNode); q-> front-> next = NULL;} // create a queue void create_Queue (LinkQueue * q) {int n = 0; init_Queue (q); printf ("Enter the elements to enter the queue and end with 0! \ N "); while (scanf (" % d ", & n) {if (n = 0) break; en_Queue (q, n );}} // e element void de_Queue (LinkQueue * q, QElemType * e) {if (q-> front = q-> rear) return; queuePtr temp = q-> front-> next; if (q-> front-> next = q-> rear) q-> rear = q-> front; * e = temp-> data; q-> front-> next = temp-> next; free (temp );} // determine whether the queue is empty int is_Empty (LinkQueue * q) {if (q-> front = q-> rear) return 1; return 0 ;} // return queue int getlength_Queue (LinkQueue * q) {QueuePt R temp = q-> front; int I = 0; while (temp! = Q-> rear) {++ I; temp = temp-> next;} return I;} // clear the queue void clear (LinkQueue * q) {QueuePtr temp = q-> front-> next; while (temp) {QueuePtr tp = temp; temp = temp-> next; free (tp );} temp = q-> front; q-> front = q-> rear = NULL; free (temp);} // print the queue element void print_Queue (LinkQueue * q) {if (q-> front = q-> rear) return; QueuePtr temp = q-> front-> next; while (temp! = Q-> rear) {printf ("% d", temp-> data); temp = temp-> next;} printf ("% d ", temp-> data); printf ("\ n");} // the first data team void top_Queue (LinkQueue * q, QElemType * e) {if (q-> front = q-> rear) return; * e = q-> front-> next-> data;} int main () {int I = 0, k = 0, top = 0; int len; LinkQueue q; create_Queue (& q); top_Queue (& q, & top ); printf ("the element of the queue header is: % d \ n", top); len = getlength_Queue (& q); printf ("all elements in the traversal queue: \ n "); for (I = 0; I <len; I ++) {de_Queue (& q, & k); printf ("% d", k );} printf ("\ n"); clear (& q); return 0 ;}