Look at the data structure write code (16) Sequential queue implementation (circular queue)

Source: Internet
Author: User

The basic structure of the loop queue is as follows:


The front property represents the team header, and the rear property represents the tail of the team.

When the team is empty: Q.rear and Q.front are all 0, the rest of the time Q.rear point to the successor of the tail, Q.front points to the head of the team.

When inserting elements at the end of a queue, Q.rear + 1, when deleting a team header element, Q.front + 1, this operation can cause a "false overflow" problem.

Figure (d) is a false overflow problem, q.rear points to the boundary of the space, and then insert will cause overflow, but in fact the space is not full.

To resolve false overflows, consider the queue as a ring. Below:


There are two issues when the queue becomes a circular queue:

1. When the team is empty and the team is full, Q.front is equal to Q.rear, which makes it impossible to distinguish between the team's empty and the full. There are two ways to solve this problem:

(1) Set a flag to distinguish whether the team is empty or the team is full.

(2) Less one element space (the highest address element), judging the team head pointer in the next position of the tail of the team is the team full.

The following code uses the first method, sets a queue Length Len property to determine the team is empty and the team is full.

2. When the team is full, you cannot reassign a larger space like a sequential table. So if you don't know the maximum space for a queue, use a linked list, or a non-circular queue. When the non-circular queue is out of the team, the head position is fixed, but the position of the element is moved and the tail position is reduced by 1.


Post My code below and welcome to the point where the code is insufficient

SequenceQueue.cpp: The algorithm implementation of the cyclic queue. #include "stdafx.h" #include <stdlib.h>////queue max number of allocations # define queue_max_size10//in order to test deliberately set values very small typedef int Elementtype;enum e_state{e_state_error = 0,e_state_ok,};//cyclic queue data structure struct Queue{elementtype * base;//heap allocation base address int front;// Team Head Index, point to the Team head node int rear;//Tail Index, point to the successor of the tail node int len;//the Captain}; E_state queueinit (Queue * queue) {queue->base = (ElementType *) malloc (sizeof (elementtype) * queue_max_size); if ( Queue->base = NULL) {Queue->front = Queue->rear = 0;queue->len = 0;return e_state_ok;} Else{return E_state_error;}} void Queueclear (Queue * q) {Q->front = Q->rear = 0;q->len = 0;} void Queuedestory (Queue * q) {queueclear (q); free (q->base); q->base = NULL;} BOOL Queuempty (Queue q) {return Q.len = = 0? true:false;} int Queuelen (Queue q) {return q.len;} Queued E_state enqueue (Queue * Q,elementtype data) {if (Q->len >= queue_max_size) {printf ("Team full, insert invalid \ n"); return e_state _error;} Q->base[q->rear] = Data;q->rear = (q->rear + 1)% QUEUE_MAX_SIZE;Q-&GT;len++;return E_STATE_OK;} Out Team E_state dequeue (Queue * q,elementtype * data) {if (Q->len > 0)//Stack not empty {*data = q->base[q->front];q-> Front = (q->front + 1)% Queue_max_size;q->len--;return E_STATE_OK;} return e_state_error;} void Queuetraverse (Queue q) {printf ("--------queue traversal starts----------\ n"); if (Q.len > 0)//queue non-empty {int index = q.front;//with Do While excluding the situation of the team full ... do{printf ("----------%d-----------\ n", Q.base[index]); index = (index+1)%queue_max_size;} while (Index! = q.rear);} printf ("--------queue traversal ended----------\ n");} int _tmain (int argc, _tchar* argv[]) {Queue queue;queueinit (&queue);//Initialize Enqueue (&queue,1);//Queue ElementType Data;dequeue (&queue,&data);//out team for (int i = 1; I <= queue_max_size+1; i++)//intentionally filled, also queue {enqueue (&queue,i);} Dequeue (&queue,&data);d equeue (&queue,&data);///Exit Queuetraverse (queue); char * s = queuempty (queue)? "NULL": "NOT NULL";p rintf ("Queue Length:%d\n queue is empty:%s", Queuelen (queue), s); Queuedestory (&queue); return 0;}


Run:










Look at the data structure write code (16) Sequential queue implementation (circular 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.