1#include <iostream>2#include <cstring>3 using namespacestd;4 #defineSize 105 structSqueue6 {7 intQueue[size];8 intfront,rear;9 };Ten voidInitqueue (Squeue *sq) One { ASq->front=sq->rear=0; - } - intIsEmpty (Squeue *sq) the { - if(sq->front==sq->rear) - return 1; - return 0; + } - intEnqueue (Squeue *sq,intEintde) + { A at if(de==0) - { - if(sq->front==0) - { - return 0; - } in Else - { tosq->front--; +sq->queue[sq->front]=e; - } the } * Else $ {Panax Notoginseng if(sq->rear==size) - { the return 0; + } A Else the { +sq->queue[sq->rear]=e; -sq->rear++; $ } $ } - } - intOuqueue (Squeue *sq,int&e,intde) the { - if(IsEmpty (sq))Wuyi return 0; the if(de==0) - { WuE=sq->queue[sq->Front]; -sq->front++; About } $ Else - { -e=sq->queue[sq->rear-1]; -sq->rear--; A } + } the intMainintargcChar Const*argv[]) - { $ squeue S; theInitqueue (&s); the inti,e; the for(i=0;i<5; i++) theEnqueue (&s,i,1); - for(i=0;i<5; i++) in { theOuqueue (&s,e,0); thecout<<e<<' '; About the } thecout<<Endl; the for(i=Ten; i>=6; i--) + { -Enqueue (&s,i,0); the }Bayi for(i=0;i<5; i++) the { theOuqueue (&s,e,1); -cout<<e<<' '; - } the return 0; the}
Dual-ended queues