1#include <iostream>2 #defineQueueelementtype int3 using namespacestd;4 5 /*Knot Point*/6typedefstructNode7 {8Queueelementtype data;/*data fields*/9Node * NEXT;/*pointer Field*/Ten }linkqueuenode; One A /*Queue*/ - structLinkqueue - { theLinkqueuenode * FRONT;/*Head Pointer*/ -Linkqueuenode * REAR;/*Tail Hands*/ - }; - + /*Initialize Q to an empty chain queue*/ - intInitqueue (Linkqueue *Q) + { AQ->front= (Linkqueuenode *)malloc(sizeof(Linkqueuenode)); at if(q->front!=0) - { -Q->rear = q->Front; -q->front->next=0; - return true; - } in Else - return false; to } + - /*determine if the queue Q is empty*/ the intIsEmpty (Linkqueue *Q) * { $ if(q->front==q->rear)Panax Notoginseng return true; - return false; the } + A /*Insert the data element x into the queue*/ the intEnterqueue (Linkqueue *Q, Queueelementtype x) + { -Node * temp = (node *)malloc(sizeof(Node)); $Temp->next =0; $Temp->data =x; -Q->rear->next =temp; -Q->rear =temp; the return true; - }Wuyi the /*Queue Q's team head element out of the team and into the storage space referred to by x*/ - intDeletequeue (Linkqueue * Q, Queueelementtype &x) Wu { - if(q->rear!=q->front) About { $x = q->front->next->data; -Q->front=q->front->Next; - return true; - } A return false; + } the - /*gets the element of the head of the team, coexisting in the storage space referred to by x*/ $ intGetHead (Linkqueue * Q, Queueelementtype &x) the { the if(q->rear==q->front) the return false; the Else -x = q->front->next->data; in return true; the } the About /*set queue q to Empty*/ the voidClearqueue (Linkqueue *Q) the { theQ->front=q->Rear; +q->front->next=0; - } the Bayi intMain () the { theLinkqueue * queue=NewLinkqueue; - initqueue (queue); -cout << IsEmpty (queue) <<Endl; the for(intI=0;i<1; i++) the Enterqueue (queue,i); thecout << IsEmpty (queue) <<Endl; the intx =-1; - Deletequeue (queue,x); thecout <<"x ="<< x <<Endl; the Deletequeue (queue,x); the inty =-1;94 GetHead (queue,x); thecout <<"y ="<< y <<Endl; thecout << IsEmpty (queue) <<Endl; the clearqueue (queue);98cout << IsEmpty (queue) <<Endl; About return 0; -}
C + + chained queue basic operations