Paper discussion: queue)

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

Queue)Is another simple and common data structure. A queue is also a set of ordered elements. The biggest feature of the queue isFirst in, first out(FIFO, first-in-first-out). This is an interesting comparison with Stack. Queues are common in life, queuing for tickets, waiting for a car ...... First, the person gets the service and leaves the queue. Then, the person joins the end of the queue. Queue is a fair way to allocate limited resources, so that the queue users can get the service at a similar waiting time.

 

The queue supports two operations, the first element of the queue.Dequeue), And new elementsEnqueue).

Queue

 

Queues are widely used in computers. A typical application is Message Queue (refer to Linux inter-process communication). In fact, a queue is used to allocate limited processes. There are also FIFO files (Oh, as you can see, these files are called FIFO and must be related to the queue) to implement pipeline transmission. For example, when we send multiple print tasks to the printer, the printer also uses the queue to arrange the task order.

  Queue C implementation (Table-based)

Similar to stack, queues can also be implemented in multiple ways. This is based on a single-chain table.

The implementation method is slightly different from that in the table (list). Here, the head node has two pointers, one (next) points to the next element, and the other (end) points to the last element of the queue. The purpose of this operation is to help us find the end of the team and perform the enqueue operation conveniently. We can still use the previously defined table to traverse and search for the last element as needed.

Table used for queue

Below isCode:

 /*  By vamei  */  /*  Use Single-linked list to implement queue  */  # Include <Stdio. h> # Include <Stdlib. h> Typedef  Struct Node * Position; typedef  Int  Elementtp;  //  Point to the head node of the List Typedef Struct Headnode * Queue;  Struct  Node {elementtp element; position next ;};  /* * Cautious: "headnode" is different from "Node", * it's another struct * end: points to the last value in the queue  */  Struct  Headnode {elementtp element; position next; Position end ;};  /*  * Operations  */  Queue init_queue (  Void  );  Void  Delete_queue (Queue );  Void Enqueue (queue, elementtp); elementtp dequeue (Queue );  Int  Is_null (Queue );  /*  * Test  */  Void Main ( Void  ) {Elementtp;  Int  I; queue qu; Qu = Init_queue (); enqueue (qu,  1  ); Enqueue (qu,  2 ); Enqueue (qu,  8  ); Printf (  "  Queue is null? % D \ n  "  , Is_null (qu ));  For (I = 0 ; I < 3 ; I ++ ) { = Dequeue (qu); printf (  "  Dequeue: % d \ n  " , A);} printf (  "  Queue is null? % D \ n  "  , Is_null (qu); delete_queue (qu );}  /*  * Initiate the queue * malloc the head node. * head node doesn't store valid data * head-> next is the first node in the queue.  */  Queue init_queue (  Void  ) {Queue HNP; HNP = (Queue) malloc ( Sizeof ( Struct Headnode); HNP -> Next = NULL; //  Qu-> next is the first node HNP-> end = NULL;  Return  HNP ;}  /*  * Dequeue all elements * and then delete head node  */  Void  Delete_queue (queue qu ){  While (! Is_null (qu) {dequeue (qu);} Free (qu );} /*  * Enqueue a value to the end of the queue  */  Void  Enqueue (queue Qu, elementtp value) {position NP, oldend; oldend = Qu-> End; NP = (Position) malloc ( Sizeof ( Struct  Node); NP -> Element = Value; NP -> Next = NULL;  /* If queue is empyt, then oldend is null  */      If  (Oldend) {oldend -> Next = NP ;}  Else  {Qu -> Next = NP;} Qu -> End = NP ;}  /*  * Dequeue the first value  */  Elementtp dequeue (queue qu) {elementtp element; position first, newfirst; If  (Is_null (qu) {printf (  "  Dequeue () on an empty queue  "  ); Exit (  1  );}  Else  {First = Qu-> Next; element = First-> Element; newfirst = First-> Next; Qu -> Next =Newfirst; free (first );  Return  Element ;}}  /*  * Check: queue is empty?  */  Int  Is_null (queue qu ){  Return (Qu-> next = Null );} 

 

The running result is as follows:

Queue is null? 0
Dequeue: 1
Dequeue: 2
Dequeue: 8
Queue is null? 1

 

Summary

Queue, FIFO

Enqueue, dequeue

 

Welcome to the "paper discussion: algorithms and data structures" series.

 

author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please keep this statement. Thank you!

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.