[Algorithm and data structure] _ 4 _ linear structure _ queue

Source: Internet
Author: User

ProgramCodeCompilation is successful and no runtime test is performed.

 /*  This program tests the Linear Logic Structure: queue  */  # Include <Stdio. h> # Include <Stdlib. h> Struct  Queuelist {  Int * Queue;  Int  Front;  Int  Rear; Int  Size ;}; typedef  Struct  Queuelist queue; typedef  Enum  {False, true} bool;  Void Createqueue (queue * queue, Int  Size); bool isqueueempty (queue * Queue); bool isqueuefull (queue * Queue); bool enqueue (queue * Queue, Int  Element); bool dequeue (queue * Queue,Int * Element );  Int Main ( Int Argc, Char * Argv []) {queue; createqueue ( & Queue, 10  );  If (Isqueueempty (& Queue) puts (  "  Empty queue. \ n  "  ); Getchar (); Return   0  ;}  /*  Function: Create a queue function prototype: void createqueue (queue * queue, int size) function parameter: queue * queue: pointer int size of the queue to be created: size of the queue to be created return value: no exception: Pass pointer  */  Void Createqueue (queue * queue, Int  Size ){  If (Queue! = NULL & size> 0  ){  If (Queue-> queue = (Int *) Malloc (size + 1 )* Sizeof ( Int  ) {Queue -> Front = Size; queue -> Rear = Size; queue -> Size = Size ;}  Else  {Puts (  "  Error. \ n  " ); Exit (  0  );}}  Else  {Puts (  "  Error. \ n  "  ); Exit (  0  );}}  /*  Function: determines whether the queue is empty. The function prototype is bool isqueueempty (queue * Queue). function parameter: queue * queue: The returned value of the queue pointer to be judged. If it is null, true is returned, otherwise, a false exception is returned. a null pointer is passed.  */ Bool isqueueempty (queue * Queue ){  If (Queue & queue-> Queue ){  If (Queue-> front = queue-> Rear ){  Return  True ;}  Else  {  Return  False ;}}  Else {Puts (  "  Error. \ n  "  ); Exit (  0  );}}  /*  Function: determines whether the queue is full. function prototype: bool isqueuefull (queue * Queue) function parameter: queue * queue: queue pointer returned value: true if the queue is full, otherwise, a false exception is returned. a null pointer is passed.  */  Bool isqueuefull (queue * Queue ){  If (Queue & queue->Queue ){  If (Queue-> rear = 0  ){  Return  True ;}  Else  {  Return  False ;}}  Else  {Puts (  "  Error. \ n  " ); Exit (  0  );}}  /*  Function: the prototype of the inbound function: bool enqueue (queue * queue, int element) function parameter: queue * queue: queue pointer to be queued int element: Elements to be queued  */  Bool enqueue (queue * Queue, Int  Element ){  If (Null = queue | null = queue-> Queue) {puts (  "  Error. \ n  " ); Exit (  0  );}  //  If the queue is empty      If  (Isqueueempty (Queue )){  If (Queue-> front = 0  ){  //  Left blank after calculation Queue-> front = queue-> Size; queue -> Queue [queue-> front] = Element; Return  True ;}  Else  {  //  The queue is empty in the initialization status. Queue-> queue [queue-> front] = Element; queue -> Rear -- ;  Return  True ;}}  Else  {  If  (Isqueuefull (Queue )){ Return  False;} queue -> Queue [queue-> rear] = Element; queue -> Rear -- ;  Return  True ;}}  /*  Function: Departure function prototype: bool dequeue (queue * queue, int * element) function parameter: queue * queue, the returned value of the element value is stored. True is returned if the team is successfully displayed. False is returned if the team fails and * element = 0 is set;  */  Bool dequeue (queue * Queue, Int *Element ){  If (Null = queue | null = queue-> queue | null = Element) {puts (  "  Error. \ n  "  ); Exit (  0  );}  If  (Isqueueempty (Queue )){  //  The queue is empty. * Element = 0  ; Return  False ;}  Else  {  //  The queue is not empty. * Element = queue-> queue [queue-> Front]; queue -> Front -- ;  Return  True ;}} 

 

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.