Sequential storage structure of C + + implementation queues for data structures (circular queues)

Source: Internet
Author: User

Queues are linear tables that allow only inserts at one end and deletions at the other end. is an advanced first-out linear table (FIFO). The one end allowed to be inserted is called a team tail, which allows the deletion of one end to be called the team head. We found in the sequential storage structure of stacks, stack operation of the top pointer in the push when the increase in the pop and reduce the stack space can be reused, and the queue of front, rear pointers have been increasing, although the previous elements have been out of the team, but it occupies the storage space can not be reused. But most programs do not use queues in this way, and in general, the elements of the team are no longer preserved, and the storage space of these elements should be recycled, thus thinking of transforming the queue into an annular queue (circular queue): Imagine the array of queues as a circle, front and The rear pointer is still growing, and automatically goes back to the array at the end of the array, like two people running around the playground, looking in the direction they run, from front to rear is a valid element of the queue, from rear to front is an empty storage location, If the front catch up with rear means the queue is empty, if rear catch up with the front, it means the storage space of the queue is full. Therefore, we generally implement it as a circular queue, when out of the queue does not need to move all, only need to modify the team head pointer, can also solve the "false overflow" problem.

Sample program: (Adapted from "liar data Structure")

#include <iostream> using namespace std;
    
#define MAXSIZE typedef int ELEMTYPE;
    typedef struct {elemtype data[maxsize]; int front; /* head pointer */INT rear;
    
/* tail pointer, if the queue is not empty, point to the end of the queue elements of the next position * * SQQUEUE;
    BOOL Initqueue (Sqqueue *pq) {cout << "Init Queue ..." << Endl;
    Pq->front = 0;
    pq->rear = 0;
return true;
    BOOL Clearqueue (Sqqueue *pq) {cout << "clear Queue ..." << Endl;
    Pq->front = 0;
    pq->rear = 0;
return true;
    BOOL Queueempty (sqqueue sq) {return (Sq.front = = sq.rear);/* Queue empty flag/} int queuelength (Sqqueue Sq) {
* * The current length of the queue/return (Sq.rear-sq.front + MAXSIZE)% MAXSIZE;
    }/* Return header element/bool GetHead (Sqqueue Sq, Elemtype *pe) {if (Queueempty (SQ)) returns false;
        else {*pe = Sq.data[sq.front];
        cout << "Get head Item" << *pe << Endl;
    return true; } bool EnQueue (Sqqueue *PQ, Elemtype ELEM) {/* Queue full */if (queuelength (*PQ) = = MAXSIZE) return false;
    cout << "EnQueue Item" << Elem << Endl; Pq->data[pq->rear] = elem;/* assigns the element to the team tail/pq->rear = (pq->rear + 1)% maxsize;/* The pointer moves back one position, * * if to
Finally go to the array head/return true;
    BOOL Dequeue (Sqqueue *PQ, Elemtype *pe) {if (Queueempty (*PQ)) return false;
    *pe = pq->data[pq->front];/* Assign the team head element to *pe/cout << "dequeue Item" << *pe << Endl;
    BOOL Queuetraverse (Sqqueue Sq) {cout << "Queue Traverse ..." << Endl; for (int i = 0; Sq.front + i < sq.rear;
    i = (i + 1)% MAXSIZE) cout << sq.data[i + sq.front] << ";
    cout << Endl;
return true;
    int main (void) {Sqqueue Sq;
    Initqueue (&SQ); for (int i = 0; i < 5; i++) EnQueue (&Amp
    Sq, i);
    Queuetraverse (SQ);
    int result;
    GetHead (Sq, &result);
    Dequeue (&SQ, &result);
    Dequeue (&SQ, &result); if (!
    Queueempty (SQ)) cout << "Queue Length:" << queuelength (sq) << Endl;
    
    Queuetraverse (SQ);
return 0; }

The output is:

Sequential storage alone, if not a circular queue, the algorithm's time performance is not high, but the loop queue is also facing the problem of possible overflow of the group.

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.