C + + chained queue basic operations

Source: Internet
Author: User

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

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.