ループキュー実現コード(ループキュー)、1~nの報数、mまで数え、前の1~mに列を出し、m対応要素をキューの尾に加え、ループを繰り返してキューを出る.C言語実装、完全コード、VSコンパイル、データ構造参照コード、実用コード、関数ライブラリ、汎用参照コード、報数サイクルキュー実装


/*         Cyclic Queue
* q.front->[  ][]->[][]->[][]->....[  ][NULL];
* q.rear->.............................|
*      q->front == q->rear
*      (q->rear + 1)%MAXSIZE == q->front (        ,           )
*/
#include 
#include 
#include 

#define DPRINT(x,...)   printf("%s line %d:",__FILE__,__LINE__); printf(x, __VA_ARGS__)

/*      */
typedef int elementype; //     

#define MAXSIZE     (100+1) //       100,    

typedef struct linkque
{
    elementype  *base;  //        
    int front;          //   
    int rear;           //   
} CyclicQueType;        //         

/****************************************************************************************
*     : QueInit
*     :      
*     : q            
*     :  
*      : 1   ,<=0   
****************************************************************************************/
int QueInit(CyclicQueType *q)
{
    q->base = (elementype*)malloc(MAXSIZE*sizeof(elementype));   //      
    if (!q->base)
    {
        DPRINT("owerlow
"); return 0; } q->front = 0; q->rear = 0; // return 1; } /**************************************************************************************** * : QueEmpty * : * : q * : * : 1 ,0 ****************************************************************************************/ int QueEmpty(CyclicQueType *q) { if (q->front == q->rear) { return 1; } else { return 0; } } /**************************************************************************************** * : QueAdd * : * : q * e * : * : > 0 ,<=0 * ,( +1)%MAX= ****************************************************************************************/ int QueAdd(CyclicQueType *q, elementype e) { if ((q->rear + 1)%MAXSIZE == q->front) { DPRINT("Full error
"); return 0; } q->base[q->rear] = e; q->rear = (q->rear + 1) % MAXSIZE; return 1; } /**************************************************************************************** * : QueDel * : * : q * : * : > 0 ****************************************************************************************/ int QueDel(CyclicQueType *q, elementype *e) { if (q->front == q->rear) { return 0; } *e = q->base[q->front]; q->front = (q->front + 1) % MAXSIZE; return 1; } /**************************************************************************************** * : QueLength * : * : q * : * : >= 0 ****************************************************************************************/ int QueLength(CyclicQueType *q) { return (q->rear + MAXSIZE - q->front)%MAXSIZE; } /**************************************************************************************** * : QueClear * : * : q * : * : >= 0 ****************************************************************************************/ int QueClear(CyclicQueType *q) { if (q->base) { free(q->base); q->base = NULL; } q->front = 0; q->rear = 0; return 1; } // 1~n , m, 1~m , m , int main(void) { CyclicQueType q; int i,n,m,j,a; elementype e; a = QueInit(&q); // scanf("%d %d", &n, &m); // n m for (i = 1; i <= n; i++) { QueAdd(&q, i); // 1~n } printf("Que len=%d
", QueLength(&q)); #if 1 while(!QueEmpty(&q)) { for (j = 1; j < m; j++) { QueDel(&q,&e); printf("%d
", e); } if (!QueEmpty(&q)) { QueDel(&q,&e); QueAdd(&q, e); } } #endif QueClear(&q); // return 0; }