Implementation of C + + queue sequential queue

Source: Internet
Author: User

#include <iostream> #include <cstdlib> #include <cstdio>using namespace Std;const int initial_lize=    10000;const int Adding_size=2*initial_lize;template<class t>struct queue{T * BASE;    T * QHEAD,*QBEGIN,*QEND,*QFRONT,*QTAIL;    int front_pos,tail_pos;    int now_size,whole_size;        Queue () {base= (T *) malloc (sizeof (t) *initial_lize);        Qhead=base; Qbegin=base;        Qend=base+ (initial_lize-1);        qfront=qtail=base+1;        Front_pos=tail_pos=1;        now_size=1;    Whole_size=initial_lize;        } bool Empty () {if (qfront==qtail) return true;    else return false;        } void push (const T x) {now_size++;            if (now_size<whole_size) {*qtail=x;            qtail++;        tail_pos++;            } else{base= (t *) realloc (base, sizeof (T)) *whole_size*2);//Do not forget the type conversion whole_size*=2; Qhead=base;            Qend=base+ (whole_size-1); Qfront=base+front_pos; Qtail=base+tAil_pos; *qtail=x;        qtail++;tail_pos++; }} T Top () {if (!        Empty ()) return (*qfront);        else{cerr<< "Queue is empty! can not return any elements" <<endl; }} void Pop () {if (!            Empty ()) {qfront++;        front_pos++;        } else{cerr<< "Queue is empty! can not pop any elements" <<endl;    }} void Delete_queue () {free (base); }};int Main () {///queue Test//queue<int> que;//int temp=0;//que.        Top ();//Que.pop ();//for (int i=0;i<10;i++) {//Que.push (i);//}//for (int i=0;i<10;i++) {// Temp=que.    Top ();//Que.pop ();//cout<<temp<<endl;//}//que.delet_queue (); return 0;}


Implementation of C + + queue sequential queue

Related Article

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: 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.