#include <stdio.h>#include<stdlib.h>#defineInit_size 1000typedefstruct { inthead,tail,size,__size,*seq;} Queue;typedef Queue*q_p;voidinit (q_p q) {q->head=q->tail=0; q->__size=init_size;q->size=0; Q->seq= (int*)malloc(init_size*sizeof(Queue));}voidPush (Q_p Q,intele) { if(q->tail+1>=q->__size) {Q->seq= (int*)realloc(Q->seq,sizeof(int) * (q->__size*=2)); } q->seq[q->tail++]=Ele; Q->size++; //Debug//printf ("PUSH%d size:%d\n", q->seq[q->tail-1],q->size);}intempty (q_p Q) {if(Q->head==q->tail)return 1; return 0;}voidpop (q_p Q) {if(!empty (q)) q->head++,q->size--; //printf ("POP size:%d\n", q->size);}intFront (q_p Q) {if(!empty (q))returnQ->seq[q->head];}voidprint (q_p Q) {printf ("%d", Front (q));p op (q); while(q->size!=0) {printf ("%d", Front (q));p op (q);} printf ("\ n");}voidTest () {inti; Q_p Q= (q_p)malloc(sizeof(Queue)); Init (q); for(i=0;i<Ten;++i) {push (q,i);} Print (q); }voidsolve () {intN,I,T;SCANF ("%d",&u); while(t--) {scanf ("%d",&N); Q_p a= (q_p)malloc(sizeof(Queue)), b= (q_p)malloc(sizeof(Queue));//or queue *a,*b;Init (a); init (b); for(i=1; i<=n;++i) {push (A,i);p ush (b,i); } //print (a);p rint (b); //Print the queue, but at the same time the action does clear the queue if(a->size!=0&&a->size<=3) print (a); intCnt=0; while(a->size>3|| B->size>3){ intK; for(k=2; k<=3;++k) {CNT++; if(cnt&1){ while(b->size!=0) pop (b); intCc=0; while(a->size!=0) {if(++cc%k!=0) Push (B,front (a));p op (a); } Else{ while(a->size!=0) Pop (a); intCc=0; while(b->size!=0) {if(++cc%k!=0) push (A,front (b));p op (b); } if(a->size!=0&&a->size<=3) {print (a); Break;} if(b->size!=0&&b->size<=3) {print (b); Break;} } } } }intMain () {//test ();solve (); return 0;}
Manual data Structure Series-C language analog queue hdu1276