Data structure: Loop queue (C language Implementation)

Source: Internet
Author: User

There are a lot of shadow in the queue, such as rice queue, train ticket queuing problem, can say that time-related problems, generally involved in the queue problem; from life, can abstract the concept of the queue, the queue is a can achieve "FIFO" storage structure. Queues are divided into chain queues and static queues, and static queues are usually implemented in arrays, but the queue must be a circular queue at this time, otherwise it will cause huge memory waste; Chained queues are implemented using linked lists. Here is the circular queue, first of all we must clear the following several issues

First, the basic knowledge of the cyclic queue

1. The loop queue requires several parameters to determine

Loop queue requires 2 parameters, front and rear

2. Meaning of the various parameters of the loop queue

(1) The front and rear values are zero when the queue is initialized;

(2) When the queue is not empty, front points to the first element of the queue, rear points to the next position of the last element of the queue;

(3) When the queue is empty, the value of front and rear is equal, but not necessarily zero;

3. The loop team is included in the team's pseudo algorithm

(1) The position where the value exists rear;

(2) rear= (rear+1)%maxsize, in which maxsize represents the length of the array;

Program code:

BOOL Enqueue (pqueue Q, int val) {if (Fullqueue (q)) return false;else{q->pbase[q->rear]=val; Q->rear= (q->rear+1)%q->maxsize;return true;}}


4. The Loop team lists the team's pseudo algorithm

(1) Save the value of the team first;

(2) front= (front+1)%maxsize, in which maxsize represents the length of the array;

Program code:

BOOL Dequeue (pqueue q, int *val) {if (Emptyqueue (q)) {return false;} else{*val=q->pbase[q->front]; Q->front= (q->front+1)%q->maxsize;return true;}}


5. How to infer if the loop queue is empty

if (front==rear)

Queue empty;

Else

The queue is not empty;

BOOL Emptyqueue (Pqueue Q) {if (q->front==q->rear)    //inferred if NULL return True;elsereturn false;}


6. How to infer if the loop queue is full


This problem is more complex, if the array of storage space is 7, this time has been stored 1,a,5,7,22,90 six elements, if you add an element to the array, then rear=front; At this point, the queue is full and the queue empty inference condition Front=rear Same, In this case we cannot infer whether the queue is empty or full;

There are two ways to solve the problem: one is to add a parameter to record the number of current elements in the array, and the second is to use less one storage space, that is, the last memory space of the array, and when (rear+1)%maxsiz=front, the queue is full;

BOOL Fullqueue (Pqueue Q) {if (q->front== (q->rear+1)%q->maxsize)    //infer that the loop list is full, leaving a reserved space without return true; Elsereturn false;}


Appendix:

Queue.h File Code:

#ifndef __queue_h_#define __queue_h_typedef struct QUEUE {int *pbase;int front;    Points to the first element of the queue int rear;    The next element that points to the last element of the queue int maxsize; Maximum storage space for the loop queue}queue,*pqueue;void createqueue (pqueue q,int maxsize), void Traversequeue (Pqueue Q); bool Fullqueue ( Pqueue q); bool Emptyqueue (Pqueue q); bool Enqueue (pqueue q, int val); bool Dequeue (pqueue q, int *val); #endif


QUEUE.C File Code:

#include <stdio.h> #include <stdlib.h> #include "malloc.h" #include "queue.h"/*************************** Function:create a empty Stack;************************************************/void Createqueue (Pqueue q,int maxsize) {q->pbase= (int *) malloc (sizeof (int) *maxsize), if (null==q->pbase) {printf ("        Memory allocation Failure "); exit (-1);         Exit Program}q->front=0; Initialize the number of parameters q->rear=0; Q->maxsize=maxsize;} /***********************************************function:print the Stack element;******************************** /void traversequeue (Pqueue Q) {int i=q->front;printf ("The element in the team is: \ n"); while (i%q->maxsize!=q-> Rear) {printf ("%d", Q->pbase[i]); i++;} printf ("\ n");} BOOL Fullqueue (Pqueue Q) {if (q->front== (q->rear+1)%q->maxsize)//Infer if the loop list is full, leave a space reserved without return True;elsereturn false;} BOOL Emptyqueue (Pqueue Q) {if (q->front==q->rear)//inferred if NULL return True;elsereturn false;} BOOL Enqueue (pqueue Q, int val) {if (Fullqueue (q)) returnfalse;else{q->pbase[q->rear]=val; Q->rear= (q->rear+1)%q->maxsize;return true;}} BOOL Dequeue (pqueue q, int *val) {if (Emptyqueue (q)) {return false;} else{*val=q->pbase[q->front]; Q->front= (q->front+1)%q->maxsize;return true;}}


Data structure: Loop queue (C language Implementation)

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.