Queue of data structures (three)--cyclic queue

Source: Internet
Author: User

The loop queue is stored in sequential order (array), the basic idea is to store data through an array, two subscript front and rear respectively point to Team head and tail

Because of the false overflow phenomenon: the use of circular queue, and because the loop queue can not determine whether the queue is empty or full, so the loss of an element is the cost of empty to separate the queue is empty or full

Unlike the chain queue, it is:

The team head pointer (subscript) of the loop queue is not the head node, but the element that points directly to the current team header

The tail pointer (subscript) of the loop queue does not point to the last element, but to the next subscript of the last element

When the loop queue is empty, the tail and the team head subscript are pointing to ah a[0]

Definition of circular queues

An array to hold the data, two subscript to hold the team head and the tail subscript

typedef char DATATYPE;TYPEDEF struct queue{datatype elem[maxsize];int front;int rear;} Seqqueue;

Initialization of the cyclic queue

Team head subscript and tail subscript are initialized to 0

Initqueue (seqqueue* Q) {q->front=0; q->rear=0;}

  

Queued operation

First judge whether it is full: condition: (q->rear+1)%maxsize==q->front

Above is an example of sacrificing a space

BOOL Enterqueue (seqqueue* Q,datatype x) {if ((q->rear+1)%maxsize==q->front) {return false;} else{q->elem[q->rear]=x; Q->rear= (q->rear+1)%maxsize;return true;}}

  

Out-of-action

First determine whether it is empty: condition: q->front==q->rear

BOOL Deletequeue (seqqueue* q,datatype* x) {if (Q->front==q->rear) {return false;} else{*x=q->elem[q->front]; Q->front= (q->front+1)%maxsize;return true;}}

  

Queue of data structures (three)--cyclic queue

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.