Sequential-Queue C + + implementation

Source: Internet
Author: User

"' #include <iostream>using namespace std;
const int MAXSIZE = 1000;typedef int elemtype;const int N = 10;typedef struct {elemtype data[maxsize];    int head; int tail;} Queue;
Queue q;void initqueue (queue &q); void PrintQueue (queue &q); int isqueueempty (queue &q); int Isqueuefull ( Queue &q); int EnQueue (queue &q,elemtype e); int DeQueue (queue &q,elemtype &e);
int main () {

for (int i=0;i<n;i++) {EnQueue (q,i);    } printQueue (Q); return 0;}

void Initqueue (Queue &q) {
Q.head = 0; Q.tail = 0;}    void PrintQueue (Queue &q) {elemtype e;        while (!isqueueempty (Q)) {DeQueue (q,e);    cout<<e<< "\ t"; }}int isqueueempty (Queue &q) {return q.head==q.tail;}
int Isqueuefull (Queue &q) {return (q.tail+1)%maxsize = = Q.head;}
int EnQueue (Queue &q,elemtype e) {
if (Isqueuefull (Q)) return 0;    Q.tail = (q.tail+1)%maxsize;    Q.data[q.tail] = e; return 1;}    int DeQueue (Queue &q,elemtype &e) {if (Isqueueempty (Q)) return 0;    Q.head = (q.head+1)%maxsize;    e = Q.data[q.head]; return 1;} "* * Note that the initialization of sequential queues can be q.head=q.tail=0 or q.head=q.tail=1; the former is equivalent to the chain queue of the lead node; the latter is equivalent to a chain queue without a lead node * *

From for notes (Wiz)

Sequential-Queue C + + 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.