Queue array and linked list implementation

Source: Internet
Author: User

Queue array and linked list implementation

Single-Chain queue Table Implementation

Queue. h

 # Ifndef queue_h _  # Define Queue_h _ Typedef  Int  Elementtype; # ifndef _ queue_list _  # Define _ Queue_list _ Struct  Qnode; typedef  Struct Qnode * Qnodeptr;  Struct Qnode {elementtype element; qnodeptr next;} qnode; typedef  Struct  Node {qnodeptr front; qnodeptr rear;} node, * Ptrtonode; typedef ptrtonode queue;  Int Isempty (queue Q ); //  Determine whether the queue is empty Q-> rear = Q-> front; Queue createqueue (); //  Create a queue  Void Disposequeue (queue Q ); // Delete the entire queue and recycle Space  Void Makeempty (queue Q ); //  Release the queue space and leave it empty.  Void Enqueue (elementtype X, queue Q ); //  Insert an element at the end of the queue Elementtype Front (queue Q ); //  Retrieve the first element, but do not delete it.  Void Dequeue (queue Q ); //  Delete an array element Elementtype frontanddequeue (queue Q );//  Void Printqueue (queue Q ); //  Print queue  # Endif  # Endif /* Queue_h _*/

Queue. c

# Include "  Queue. h  "  # Include <Stdio. h> # Include <Stdlib. h> Int  Isempty (queue q ){  Return Q-> rear = Q-> Front ;}  Void  Makeempty (queue q ){  If (Q = Null) {printf (  "  The queue is empty! Must use createqueue first! \ N  "  );  Return  ;}  Else  {  While (!Isempty (q) dequeue (q) ;}} queue createqueue (){  //  You must not only apply for space for the queue, but also for the queue element/node (qnode.  Queue Q; q = (Queue) malloc ( Sizeof ( Struct  Node ));  If (Q = Null) {printf (  "  Out of space! \ N  "  );  Return NULL;} Q -> Front = Q-> rear = (qnodeptr) malloc ( Sizeof ( Struct  Qnode ));  If (Q-> front = Null) {printf (  "  Out of space! \ N  "  );  Return  NULL;} Q -> Front-> next = NULL;  Return Q ;}  Void  Disposequeue (queue q ){  While (Q-> front! = Null) {q -> Rear = Q-> front-> Next; free (Q -> Front); q -> Front = Q-> Rear ;}}  Void  Dequeue (queue q ){  //  Delete the first element of a chain queue      If (!Isempty (q) {qnodeptr P; P = Q-> front-> Next; q -> Front-> next = p-> Next;  If (Q-> rear = P) //  Determine whether there is only one element in the queue Q-> rear = Q-> Front; free (p );}  Else  {Printf (  "  The queue is empty! \ N  "  );}} Void  Enqueue (elementtype X, queue q) {qnodeptr P = (Qnodeptr) malloc ( Sizeof ( Struct  Qnode ));  If (P = Null) {printf (  "  Out of space! \ N  "  );  Return  ;}  Else  {P -> Next = NULL; P -> Element = X; q -> Rear-> next = P; q -> Rear = P ;}} elementtype Front (queue q ){  Return Q-> front-> next-> Element;} elementtype frontanddequeue (queue q) {elementtype x = 0  ;  If (! Isempty (q) {x = Front (Q); dequeue (Q );} Else  {Printf (  "  The queue is empty! \ N  "  );}  Return  X ;}  Void  Printqueue (queue q) {qnodeptr P = Q-> Front;  While (P! = Q-> Rear) {P = P-> Next; printf (  " % D \ n  " , P-> Element );}} 

Linkqueue. c

# Include <stdio. h># Include"Queue. h"IntMain (Void) {Queue Q; q=Createqueue ();IntI;For(I =0; I <10; I ++) {Enqueue (I, q);} printqueue (Q );Return 0;}

Create a single-chain table header Node

Loop Array Implementation

Cyclic queues are generally implemented using cyclic arrays. The empty condition can be determined in multiple ways: one is to set a flag to distinguish whether the queue is empty or full; the other is to use less element space, this Convention uses the queue header pointer at the next position of the queue's tail pointer as a sign that the queue is full. Or, as shown in the following code, set a size flag indicating the queue size for the struct. If the size is 0, the queue is empty.

Queue. h

 # Ifndef _ queue _  # Define _ Queue _ Typedef  Int  Elementtype; typedef  Struct  Queuerecord {  Int  Capacity;  Int  Front;  Int Rear;  Int  Size; elementtype * Array ;} * Queue;  Int  Isempty (queue Q );  Int  Isfull (queue Q); queue createqueue (  Int  Maxelements );  Void  Disposequeue (queue Q );  Void  Makeempty (queue Q ); Void  Enqueue (elementtype X, queue Q); elementtype Front (queue Q );  Void  Dequeue (queue Q); elementtype frontanddequeue (queue Q );  # Endif 

Queue. c

# Include "  Queue. h  "  # Include <Stdio. h> # Include <Stdlib. h> Int  Isempty (queue q ){  Return Q-> size = 0  ;}  Int  Isfull (queue q ){  Return Q-> size = Q-> Capacity ;}  Void  Makeempty (queue q) {q -> Size = 0  ; Q -> Front = 1  ; Q -> Rear = 0  ;} Queue createqueue ( Int  Maxelements) {queue Q; q = (Queue) malloc ( Sizeof ( Struct  Queuerecord ));  If (Q = Null) {printf (  "  Out of space! \ N  "  );  Return  NULL;} Q -> Array = (elementtype *) malloc ( Sizeof (Elementtype )* Maxelements );  If (Q-> array = Null) {printf (  "  Out of space! \ N  "  );  Return  NULL;} Q -> Capacity = Maxelements; makeempty (Q );  Return  Q ;}  Void  Disposequeue (queue q ){ If (Q! = Null) {free (Q -> Array); free (q );}}  Static   Int Succ ( Int  Value, queue q ){  If (++ Value = Q-> Capacity) Value = 0  ;  Return  Value ;}  Void Enqueue (elementtype X, queue q ){  If  (Isfull (q) {printf (  "  Queue is full! \ N  "  );  Return  ;}  Else  {Q -> Size ++ ; Q -> Rear = succ (Q-> rear, q ); // Q-> rear = (Q-> rear + 1) % Q-> capacity? Q-> rear + 1: 0; Q-> array [q-> rear] = X ;}} elementtype Front (queue q ){  If (! Isempty (q )){  Return Q-> array [q-> Front];} printf (  "  Queue is empty! \ N  "  );  Return   0  ;} Void  Dequeue (queue q ){  If (! Isempty (q) {q -> Size -- ; Q -> Front = succ (Q-> Front, q );}  Else  {Printf (  "  Queue is empty! \ N  "  ) ;}} Elementtype frontanddequeue (queue q) {elementtype x = 0 ;  If (! Isempty (q) {q -> Size -- ; X = Q-> array [q-> Front]; q -> Front = succ (Q-> Front, q );}  Else  {Printf (  "  Queue is empty! \ N  "  );}  Return  X ;} 

Main. c

# Include <stdio. h> # Include  "  Queue. h  "  Int Main ( Void  ) {Queue Q; q = Createqueue ( 10  );  Int  I, J;  For (I = 0 ; I < 10 ; I ++) {Enqueue (I, q );}  For (I = 0 ; I < 10 ; I ++ ) {J = Frontanddequeue (Q); printf (  "  % D  "  , J );}  Return   0  ;} 

The cyclic array enables the queue to determine whether the queue is empty by size, and capacity to determine whether the queue is full. The header and tail pointer only cares about basic operations. The value in the array changes after the queue is entered, and the previous value is retained.

A single-chain table implements a queue and sets the header node. the header and tail pointers all point to the header node. The linked list is empty, dynamically adding nodes and releasing space.

From http://blog.csdn.net/universitycd/article/details/8862180

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.