双端队列
1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 #define size 10 5 struct squeue 6 { 7 int queue[size]; 8 int front,rear; 9 }; 10 void initqueue(squeue *sq) 11 { 12 sq->front=sq->rear=0; 13 } 14 int isempty(squeue *sq) 15 { 16 if(sq->front==sq->rear) 17 return 1; 18 return 0; 19 } 20 int enqueue(squeue *sq,int e,int de) 21 { 22 23 if(de==0) 24 { 25 if(sq->front==0) 26 { 27 return 0; 28 } 29 else 30 { 31 sq->front--; 32 sq->queue[sq->front]=e; 33 } 34 } 35 else 36 { 37 if(sq->rear==size) 38 { 39 return 0; 40 } 41 else 42 { 43 sq->queue[sq->rear]=e; 44 sq->rear++; 45 } 46 } 47 } 48 int ouqueue(squeue *sq,int &e,int de) 49 { 50 if(isempty(sq)) 51 return 0; 52 if(de==0) 53 { 54 e=sq->queue[sq->front]; 55 sq->front++; 56 } 57 else 58 { 59 e=sq->queue[sq->rear-1]; 60 sq->rear--; 61 } 62 } 63 int main(int argc, char const *argv[]) 64 { 65 squeue s; 66 initqueue(&s); 67 int i,e; 68 for(i=0;i<5;i++) 69 enqueue(&s,i,1); 70 for(i=0;i<5;i++) 71 { 72 ouqueue(&s,e,0); 73 cout<<e<<' '; 74 75 } 76 cout<<endl; 77 for(i=10;i>=6;i--) 78 { 79 enqueue(&s,i,0); 80 } 81 for(i=0;i<5;i++) 82 { 83 ouqueue(&s,e,1); 84 cout<<e<<' '; 85 } 86 return 0; 87 }