- # Ifndef SQQUEUE_H_INCLUDED
- # Define SQQUEUE_H_INCLUDED/* prevent repeated inclusion */
-
- //////////////////////////////////////// //
- // Contains the header file
- # Include <stdlib. h>
- # Include "ds. h" // OK, Status, and other definitions
-
- // Data element type (int type is used by default)
- # Ifndef ElemType
- # Define ElemType int
- # Define USE_DEFAULT_ELEMTYPE/* use the default type flag */
- # Endif // ElemType
-
- //////////////////////////////////////// //
- // The storage structure of the cyclic queue
-
- # Define MAXQSIZE 500/* Maximum capacity of cyclic queue */
- Typedef struct {
- /* TODO (#1 #): define the type of the cyclic queue */
- ElemType * base;
- Int front;
- Int rear;
- //....................................
- } SqQueue;
-
-
- //////////////////////////////////////// //
- // Basic operations of the cyclic queue
-
- // Construct an empty queue Q
- Status InitQueue (SqQueue & Q)
- {
- /* TODO (#2 #): Create an empty queue */
- Q. base = (ElemType *) malloc (MAXQSIZE * sizeof (ElemType ));
- If (! Q. base) exit (OVERFLOW );
- QQ. front = Q. rear = 0;
- Return OK; // TODO: replace this line of code.
- //....................................
- }
-
- // Destroy queue Q
- // Prerequisite: the queue Q already exists.
- Status DestroyQueue (SqQueue & Q)
- {
- /* TODO (#3 #): destroys a queue */
- Free (Q. base );
- Q. base = NULL;
- Q. front = 0;
- Q. rear = 0;
- Return OK;
- //....................................
- }
-
- // Clear the queue Q As an empty queue
- // Prerequisite: the queue Q already exists.
- Status ClearQueue (SqQueue & Q)
- {
- /* TODO (#4 #): Clear the queue */
- Q. base = 0;
- Q. rear = 0;
- Return OK;
- //....................................
- }
-
- // If the queue Q is empty, TRUE is returned; otherwise, FALSE is returned.
- // Prerequisite: the queue Q already exists.
- Status QueueEmpty (SqQueue Q)
- {
- /* TODO (#5 #): determines whether the queue is empty */
- If (Q. front = Q. rear)
- Return OK;
- Else
- Return ERROR;
- //....................................
- }
-
- // Return the number of elements in the queue Q, that is, the queue length.
- // Prerequisite: the queue Q already exists.
- Int QueueLength (SqQueue Q)
- {
- /* TODO (#6 #): Return queue length */
- Return (q. rear-Q.front + MAXQSIZE) % MAXQSIZE;
- //....................................
- }
-
- // Use e to return the Q header element of the queue.
- // Prerequisite: the queue Q exists and is not empty.
- Status GetHead (SqQueue Q, ElemType & e)
- {
- /* TODO (#7 #): store the element from the queue header to e */
- If (Q. rear = Q. front)
- Return ERROR;
- E = Q. base [Q. front];
- // E = * (Q. base + Q. front );
- Return OK; // return the operation status (success: OK, failure: ERROR)
- //....................................
- }
-
- // Insert element e as the new team end element of queue Q
- // Prerequisite: the queue Q exists and is not full
- Status EnQueue (SqQueue & Q, ElemType e)
- {
- /* TODO (#8 #): Element e enters the queue */
- If (Q. rear + 1) % MAXQSIZE = Q. front)
- Return ERROR;
- // E = * (Q. base + Q. rear );
- Q. base [Q. rear] = e;
- Q. rear = (Q. rear + 1) % MAXQSIZE;
- Return OK; // return the operation status (success: OK, failure: ERROR)
- //....................................
- }
-
- // Delete the queue Header element of queue Q and return it with e
- // Prerequisite: the queue Q exists and is not empty.
- Status DeQueue (SqQueue & Q, ElemType e)
- {
- /* TODO (#9 #): the output queue is stored in e */
- If (Q. front = Q. rear)
- Return ERROR;
- // E = * (Q. base + Q. front );
- E = Q. base [Q. front];
- Q. front = (Q. front + 1) % MAXQSIZE;
- Return OK; // return the operation status (success: OK, failure: ERROR)
- //....................................
- }
-
- //////////////////////////////////////// //
-
-
- // TODO: Use the QueueView function after defining the SqQueue type
- /***** // TODO: delete this row to use QueueView ()
- # Include <stdio. h>
- // View the queue status (for debugging)
- Void QueueView (SqQueue Q)
- {
- Extern void PrintElem (ElemType e); // used to print data
- Int I = 0;
- If (Q. front <0 | Q. front> = MAXQSIZE | Q. rear <0 | Q. rear> = MAXQSIZE ){
- Printf ("queue not initialized \ n ");
- Return;
- }
- Printf ("--- Queue View --- \ n ");
- Printf ("front = % d, rear = % d \ n", Q. front, Q. rear );
- If (Q. rear> = Q. front ){
- Printf ("... \ n ");
- For (I = Q. front; I <Q. rear; I ++ ){
- Printf ("% 5d \ t", I );
- PrintElem (Q. base [I]);
- Printf ("\ n ");
- }
- If (I <MAXQSIZE) printf ("...... \ n ");
- } Else {
- For (I = 0; I <Q. rear; I ++ ){
- Printf ("% 5d \ t", I );
- PrintElem (Q. base [I]);
- Printf ("\ n ");
- }
- Printf ("... \ n ");
- For (I = Q. front; I <MAXQSIZE; I ++ ){
- Printf ("% 5d \ t", I );
- PrintElem (Q. base [I]);
- Printf ("\ n ");
- }
- }
- Printf ("--- view end --- \ n ");
- }
- * ***** // TODO: delete this row to use QueueView ()
-
- // Cancel the default definition of ElemType to avoid affecting other parts.
- # Ifdef USE_DEFAULT_ELEMTYPE
- # Undef ElemType
- # Undef USE_EFAULT_ELEMTYPE
- # Endif
-
- # Endif // SQQUEUE_H_INCLUDED
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include "sqqueue. h"
-
- // Initialize the system
-
-
- Void Finalize (SqQueue & q );
-
- //////////////////////////////////////// ////
- // Main program
- Int main ()
- {
- SqQueue q; // cyclic queue
- Int x;
- // System initialization
- InitQueue (q );
- Printf ("data elements in the queue, ending with 0 ");
- Scanf ("% d", & x );
- While (x! = 0 ){
- EnQueue (q, x );
- Scanf ("% d", & x );
- }
- Printf ("\ n number of queue elements ");
-
- Printf ("% d", QueueLength (q ));
-
-
- Printf ("\ nheader element :");
- If (! QueueEmpty (q )){
- If (GetHead (q, x) = OK)
- Printf ("% d", x );
- }
-
-
- Printf ("\ n outgoing queue, first-in-first-out ");
- If (DeQueue (q, x) = OK)
- Printf ("% d", x );
- Printf ("\ n :");
- If (! QueueEmpty (q )){
- If (GetHead (q, x) = OK)
- Printf ("% d \ n", x );
- }
- }
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201304/222911580.jpg "border =" 0 "alt =" "/>
This article from the "Zhao Yuqiang blog" blog, please be sure to keep this source http://zhaoyuqiang.blog.51cto.com/6328846/1179584