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 (&LQ); for (int i = 0; i < 5; i++) {EnQueue (&LQ, i); } queuetraverse (LQ); int result; GettoP (&LQ, &result); DeQueue (&LQ, &result); if (! Isemptyqueue (LQ)) {cout << "Queue Length:" << queuelength (LQ) << Endl; } queuetraverse (LQ); Destroyqueue (&LQ); return 0;}
Queue (C language Implementation)