PTA 7-1 Bank Business Queue Simple simulation

Source: Internet
Author: User

To implement a queue operation with a linked list, the code is as follows:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <malloc.h>5 6 using namespacestd;7 8 //function Status Code definition9 #defineTRUE 1Ten #defineFALSE 0 One #defineOK 1 A #defineERROR 0 - #defineINFEASIBLE-1 - #defineOVERFLOW-2 the  -typedefintStatus; -typedefintQelemtype; -  +typedefstructQnode { - qelemtype data; +     structQnode *Next; A}qnode, *queueptr; at  -typedefstruct { - queueptr Front; - queueptr Rear; - }linkqueue; -  inStatus Initqueue (Linkqueue &Q) { -Q.front = Q.rear = (queueptr)malloc(sizeof(Qnode)); to     if(!Q.front) exit (OVERFLOW); +Q.front->next =NULL; -     returnOK; the } *  $Status Destroyqueue (Linkqueue &Q) {Panax Notoginseng      while(q.front) { -cout << Q.front->data <<Endl; theQ.rear = q.front->Next; +          Free(Q.front); AQ.front =q.rear; the     } +     returnOK; - } $  $Status EnQueue (Linkqueue &Q, Qelemtype e) { -Q.rear->next = (queueptr)malloc(sizeof(Qnode)); -     if(! Q.rear->next) exit (OVERFLOW); theQ.rear->data =e; -Q.rear = q.rear->Next;WuyiQ.rear->next =NULL; the     returnOK; - } Wu  -Status DeQueue (Linkqueue &q, Qelemtype &e) { About     if(Q.front = =q.rear) $         returnERROR; -E = q.front->data; -Queueptr p =Q.front; -Q.front = q.front->Next; A      Free(p); +     returnOK; the } -  $ BOOLEmptyqueue (Linkqueue &Q) { the     returnQ.front = =q.rear; the } the  the intMain () - { in linkqueue A, B; the Initqueue (A); the Initqueue (B); About     intN, M; theCIN >>N; the      for(inti =0; I < n; ++i) { theCIN >>m; +         if(M &1) - EnQueue (A, m); the         ElseBayi EnQueue (B, m); the     } the     BOOLOK =false; -      while(! Emptyqueue (A) &&!Emptyqueue (B)) { - DeQueue (A, m); the         if(OK) thecout <<' '<<m; the         Else thecout <<m; -  the         if(!Emptyqueue (A)) { the DeQueue (A, m); thecout <<' '<<m;94         } the DeQueue (B, m); thecout <<' '<<m; theOK =true;98     } About      while(!Emptyqueue (A)) { - DeQueue (A, m);101         if(OK)102cout <<' '<<m;103         Else104cout <<m; theOK =true;106     }107      while(!Emptyqueue (B)) {108 DeQueue (B, m);109         if(OK) thecout <<' '<<m;111         Else thecout <<m;113OK =true; the     } the     return 0; the}

PTA 7-1 Bank Business Queue Simple simulation

Related Article

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.