Queue (C language Implementation)

Source: Internet
Author: User

Tag:als   oid    initialization    iostream   lib    count   false   bool   res   

#include <iostream> #include <stdlib.h>using structure implementation of namespace std;/********************************* chain Queue * *    /typedef int qelemtype;typedef struct qnode{qelemtype data; struct Qnode *next;}    Qnode, *qnodeptr;typedef struct{qnodeptr front; QNODEPTR Rear;} linkqueue;/********************************* chain Queue operation Implementation ********************************//* initialize a chain queue */bool InitQueue (Linkqueue *LQ)        {cout << "Init linkqueue ..." << Endl;    Qnodeptr p = (qnodeptr) malloc (sizeof (Qnode));    P->next = NULL;    Lq->front = Lq->rear = P; return true;}    /* Insert element e As Q's new team tail element */bool EnQueue (Linkqueue *LQ, Qelemtype e) {qnodeptr s = (qnodeptr) malloc (sizeof (Qnode));    if (!s) {exit (0);    } s->data = e;    S->next = NULL;    Lq->rear->next = s;    Lq->rear = s; return true;}    /* Delete the queue's team header element, with e return its value */bool DeQueue (linkqueue *lq, Qelemtype *e) {qnodeptr p = NULL;        if (Lq->front = = lq->rear) {return false;    } p = lq->front->next;    *e = p->data;    Lq->front->next = p->next;    if (p = = lq->rear)//If the team head is also at the end of the team, then delete the rear point to the head node */{lq->rear = lq->front;    } cout << "DeQueue Item:" << *e << Endl;    Free (p); return true;} /* Destroy queue, including head node */bool destroyqueue (Linkqueue *lq) {cout << "Destroy queue ...    "<< Endl;        while (lq->front) {lq->rear = lq->front->next;        Free (Lq->front);    Lq->front = lq->rear; } return true;    /* Clears the queue but retains the head node */bool clearqueue (Linkqueue *lq) {if (Lq->front = = lq->rear) {return true;    } cout << "Clear Queue ..." << Endl;    Qnodeptr p = lq->front->next;    Qnodeptr q = NULL;    Lq->front->next = NULL;    Lq->rear = lq->front;        while (p) {q = p->next;        Free (p);    p = q; } return true; /* Determine if the queue is empty */bool isemptyqueue (linkqueue LQ) {return Lq.front = = LQ.REar;}    /* Returns the number of nodes in the queue */int queuelength (linkqueue LQ) {int count = 0;    if (Lq.front = = NULL)//Queue not initialized {return 0;    } qnodeptr p = lq.front->next;        while (p) {p = p->next;    count++; } return count;}    /* Return queue of Team head element */bool GetTop (linkqueue *lq, Qelemtype *e) {qnodeptr p;    if (Lq->front = = Lq->rear) {return false;    } p = lq->front->next;    *e = p->data;    cout << "Get Top Item:" << *e << Endl; return true;}    /* traverse each element in the queue */bool Queuetraverse (Linkqueue LQ) {if (Lq.front = = Lq.rear) {return false;    } cout << "Queue Traverse ..." << Endl;    Qnodeptr p = lq.front->next;        while (p) {cout << p->data << ';    p = p->next;    } cout << Endl; return true;}    void Main (void) {linkqueue LQ;    Initqueue (&AMP;LQ);    for (int i = 0; i < 5; i++) {EnQueue (&AMP;LQ, i);    } queuetraverse (LQ);    int result; GettoP (&AMP;LQ, &result);    DeQueue (&AMP;LQ, &result); if (!    Isemptyqueue (LQ)) {cout << "Queue Length:" << queuelength (LQ) << Endl;    } queuetraverse (LQ);    Destroyqueue (&AMP;LQ);     return 0;}

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.