Wang books on the basic operation queue and the after-school exercise portion
A: circular queue (array implementation)
/ * * * @Author Dawn * @date 2019 Nian 11 Yue 12 Ri 20:39:27 * @version 1.0 * circular queue: The greatest feature about the circular queue is that every time the team and the team as well as whether the queue full, when they are using a mold. Instead of the usual simple calculations * / #include <stdio.h> #include <stdlib.h> #define the MaxSize. 6 typedef int elemType; typedef struct { elemType Data [the MaxSize]; // store queue element int Front, REAR; // queue head and queue tail } SeQueue; // 1. initialization void InitQueue (SeQueue & Q) { Q.rear = Q.front = 0 ; // flag, not a pointer Oh! ! } // 2. whether the queue is empty BOOL the IsEmpty (SeQueue Q) { IF (Q.front == Q.rear) return to true ; the else return to false ; } // 3. enqueued BOOL ENQUEUE (SeQueue & Q, X elemType ) { // determines whether the full iF ((+ Q.rear . 1 ) the MaxSize% == Q.front) { the printf ( " queue this stays \ n- " ); return to false ; } Q.data [Q .rear] = X; Q.rear = (+ Q.rear. 1 )% the MaxSize; return to true ; } // 4. dequeue BOOL DEQUEUE (Q & SeQueue, & elemType X) { // If blank IF (Q.rear == Q.front) return to false ; X = Q.data [ Q.front]; Q.front = (+ Q.front . 1 )% the MaxSize; return to true ; } int main () { SeQueue Q; InitQueue (Q); the printf ( " queue is empty:% S \ n- " , IsEmpty (Q)? " empty" : " Not empty " ); ENQUEUE (Q, . 1 ); ENQUEUE (Q, 2 ); ENQUEUE (Q, . 3 ); ENQUEUE (Q, . 4 ); ENQUEUE (Q, . 5 ); ENQUEUE (Q, . 5 ) ; // fail to execute printf ( " queue is empty:% S \ the n- " , IsEmpty (Q)? " empty " : " is not empty " ); // start the queue int the X-; DEQUEUE (Q, the X-) ; printf ( " a team elements:% d \ n" , X); // dequeue elements 1, FIFO return 0 ; }
II: storage queue chain
/ * * * @Author Dawn * @date 2019 Nian 11 Yue 11 Ri 22:25:13 * 1.0 @version review * queue: Linked Storage * / #include <stdio.h> #include <stdlib.h> typedef int elemType; typedef struct LinkNode { // on the books benevolent there is a small error. No write LinkNode, the error will not write elemType Data; struct LinkNode * Next; } LinkNode; typedef struct { LinkNode * Front, * REAR; // queue the queue head and queue tail pointers } LinkQueue; // 1. Initialization void InitQueue (LinkQueue & Q) { // lead node Ha Q.front Q.rear = = (* LinkNode) the malloc ( the sizeof (LinkNode)); Q.front -> Next = NULL; // initially empty } // 2. whether the queue is empty bool the IsEmpty (LinkQueue Q) { IF (Q.front == Q.rear) return to true ; the else return to false ; } // 3. enqueued void eNQUEUE (LinkQueue & Q, X elemType) { LinkNode * P = (LinkNode *) the malloc ( the sizeof (LinkNode)); P -> Data =X; // inserted into the queue Q.rear-> Next = P; Q.rear = P; } // 4. dequeue BOOL DEQUEUE (Q & LinkQueue, & elemType X) { // if the queue is empty IF (Q == .front Q.rear) return to false ; LinkNode * P = Q.front-> Next; // be dequeued element X = p-> Data; Q.front -> Next = p-> Next; IF ( == Q.rear the p-) Q.rear = Q.front; // If there is only one element, after deleting empty as Free (the p-); return to true ; } // the king with two stacks of books (s1, s2) to practice the queue // Thinking? Stack: the element into s1, if the full s1, s2 stack would have to wait for the "empty." Then s1 element "All" s2 placed in // the stack: the elements removed for s2, s2 if empty, then s1 is "all" element s2 into the int main () { LinkNode L ; LinkQueue Q; InitQueue (Q); the printf ( " whether the queue is empty:% S \ n- ' ?, the IsEmpty (Q) " empty " : " not empty " ); ENQUEUE (Q, . 1 ); ENQUEUE (Q, 2 ); ENQUEUE (Q, . 3 ); ENQUEUE (Q, . 4 ); ENQUEUE (Q, . 5); Printf ( " whether the queue is empty:% S \ the n- " , IsEmpty (Q)? " Empty " : " not empty " ); elemType the X-; DEQUEUE (Q, the X-); printf ( " the first element out was:% D \ n- " , X); DEQUEUE (Q, X); the printf ( " out of the second element is: D% \ n- " , X); return 0 ; }