[Algorithm and data structure] _ 9 _ linear structure _ queue _ continued _ 1

Source: Internet
Author: User

A section about queueSource codeYou may find thatCodeSome problems exist. That is, if the frontend points to the end of the queue after leaving the queue;

An exception occurs. Therefore, to be able to use the queue, You need to modify the source code.

 /*  * BookProgramTest linear data structure: queue  */  # Include <Stdio. h> # Include <Stdlib. h> //  ************************************* 0  //  Define data types  Struct  Queuenode { //  If rear =-1 and front =-1, the queue does not exist.  //  The queue does not exist and cannot perform any operations on the queue.      Int * Queue;  Int Front; //  First queue      Int Rear; //  End of queue      Int  Maxsize ;}; typedef  Struct Queuenode queue; typedef queue * Pqueue; typedef  Enum  {False, true} bool;  //  * ************************************ 1  //  ************************************* 0  //  Define User Constants  # Define Max_queue_size 4 //  * ************************************ 1  // ************************************* 0  Pqueue createqueue (  Void  ); Bool initqueue (pqueue Queue); bool isempty (pqueue Queue); bool isfull (pqueue Queue); bool enqueue (pqueue queue,  Int  Element); bool dequeue (pqueue queue,  Int * Element );  Int Main ( Void  ) {Pqueue queue;  Int  X; queue =Createqueue ();  If  (Initqueue (Queue) puts (  "  Yes  "  );  If  (Isempty (Queue) puts (  "  Yes  "  );  If (Enqueue (queue, 1  ) Puts ( "  En success  "  );  If (Enqueue (queue, 1  ) Puts (  "  En success  "  );  If (Enqueue (queue, 1  ) Puts (  "  En success "  );  If (Enqueue (queue, 1  ) Puts (  "  En success  " ); //  In this way, output three ensuccess instances.      If  (Isfull (Queue) puts (  "  Yes  "  ); If (Dequeue (queue ,& X) printf (  "  Dequeue success, % d \ n  "  , X );  If (Enqueue (queue, 1  ) Puts (  "  En success  "  ); Getchar ();  Return   0 ;}  //  * ************************************ 1  //  ************************************* 0  /*  Function: Create a queue function prototype: pqueue createqueue (void) function parameter: no return value: If the creation is successful, the queue storage address is returned; otherwise, an error is returned: None  */  Pqueue createqueue (  Void  ) {Pqueue queue; queue = Malloc ( Sizeof  (Queue ));  If (Queue) {queue -> Front =- 1  ; Queue -> Rear =- 1  ;  Return  Queue ;}  Else  {  Return  NULL ;}}  //  * ************************************ 1  // ************************************* 0  /*  Function: Initialize the queue function prototype: bool initqueue (pqueue Queue) function parameter: pqueue queue: returned value of the queue to be initialized: True is returned if Initialization is successful; otherwise, false is returned. Exception: None  */  Bool initqueue (pqueue Queue ){  If (! Queue)  Return  False; queue -> Queue = malloc ( Sizeof ( Int )* Max_queue_size ); If  (Queue) {queue -> Front =- 1  ; Queue -> Rear = 0  ; Queue -> Maxsize = Max_queue_size;  Return  True ;}  Else  {  Return  False ;}}  // * ************************************ 1  //  ************************************* 0  /*  Function: determines whether the queue is empty. function prototype: bool isempty (pqueue Queue) function parameter: pqueue queue: queue pointer function return value: If it is null, true is returned; otherwise, false is returned: none  */  Bool isempty (pqueue Queue ){  If (! Queue | queue-> rear =- 1  ) {Puts (  "  Error.  " ); Exit (  0  );}  Return Queue-> rear = 0 ? True: false ;}  //  * ************************************ 1  //  ************************************* 0  /*  Function: determines whether the queue is full. function prototype: bool isfull (pqueue Queue) function parameter: pqueue queue: queue pointer function return value: If it is full, true is returned; otherwise, false is returned: none  */ Bool isfull (pqueue Queue ){  If (! Queue | queue-> rear =- 1  ) {Puts (  "  Error.  "  ); Exit (  0  );}  If (- 1 = Queue-> front & queue-> rear = (queue-> maxsize- 1  )) Return  True;  Else          Return  False ;}  //  ************************************* 0  //  ************************************* 0  /*  Function: Move the queue element to-1. function prototype: void movequeue (pqueue) function parameter: pqueue queue: queue return value: no exception: None  */  Void  Movequeue (pqueue Queue ){ Int  I, J;  //  This function is only used in the queue function, and the validity is completed in the queue function. I = queue-> Front; queue -> Front =- 1  ; J = 0  ;  While (J < I) {queue -> Queue [J] = queue-> queue [I + J]; j ++ ;} Queue -> Rear = queue-> maxsize-queue-> Front ;}  //  * ************************************ 1  //  ************************************* 0  /*  Function: queuing function prototype: bool enqueue (pqueue queue, int element) function parameter: pqueue queue: queue int element: Elements to be queued return value: Successful, true is returned; otherwise, false is returned. Exception: None  */  Bool enqueue (pqueue queue,  Int  Element ){  If (! Queue | queue-> rear =-1  ) {Puts (  "  Error.  "  ); Exit (  0  );}  If  (Isfull (Queue ))  Return  False;  Else  {  /* This logic can also be used here to make it simpler if (queue-> rear = queue-> maxsize-1) movequeue (Queue ); queue-> queue [queue-> rear ++] = element; return true;  */          If (Queue-> rear! = Queue-> maxsize- 1  ){  //  If the last rear is not reached, add Queue-> queue [queue-> rear ++] = Element;  Return  True ;}  Else {  //  If the rear reaches the end and the queue is not full, move the element  //  After moving, add the element  Movequeue (Queue); queue -> Queue [queue-> rear ++] = Element;  Return  True ;}}}  //  * ************************************ 1  //  ************************************* 0 /*  Function: out-of-band function prototype: bool dequeue (pqueue queue, int * element) function parameter: pqueue queue: queue int * element: stores the element return value after the out-of-band operation: If the out-of-queue operation is successful, true is returned, and * element = the first element of the team is set. If the team fails, false is returned, and * element = 0 is set. Exception: none.  */  Bool dequeue (pqueue queue,  Int * Element ){  If (! Queue | queue-> rear =- 1  ) {Puts (  "  Error.  " ); Exit (  0  );}  If  (Isempty (Queue ))  Return  False; * Element = queue-> queue [++ queue-> Front];  Return  True ;}  //  ************************************* 0 

The code running result is as follows:

If the cyclic queue is used, the above problems will not exist.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.