#ifndef _SEQUEUE_H_
#define _SEQUEUE_H_
#define MAXSIZE 1000
#define FAILURE 100001
#define SUCCESS 100002
typedef int ElemType;
struct queue
{
ElemType data[MAXSIZE];
int front;
int rear;
};
typedef struct queue SeQueue;
int QueueInit(SeQueue *Q);
int EnQueue(SeQueue *Q,ElemType e);
int QueueLength(SeQueue Q);
ElemType GetFront(SeQueue Q);
ElemType DelQueue(SeQueue *Q);
#endif
include <stdio.h>
#include “SeQueue.h”
int QueueInit(SeQueue *Q)
{
Q->rear = Q->front = 0;
return SUCCESS;
}
int EnQueue(SeQueue *Q,ElemType e)
{
if((Q->rear +1) % MAXSIZE == Q->front)
{
return FAILURE;
}
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1)% MAXSIZE;
return SUCCESS;
}
int QueueLength(SeQueue Q)
{
return ((Q.rear – Q.front + MAXSIZE) % MAXSIZE);
}
ElemType GetFront(SeQueue Q)
{
return Q.data[Q.front];
}
ElemType DelQueue(SeQueue *Q)
{
ElemType e;
if(Q->rear == Q->front)
{
return FAILURE;
}
e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return e;
}
#include <stdio.h>
#include “SeQueue.h”
int main()
{
int ret,i,j,t,length,num;
SeQueue sq,qs;
if(QueueInit(&sq) != SUCCESS || QueueInit(&qs) != SUCCESS)
{
printf(“Init Failure!!\n”);
}
printf(“Please input num\n”);
scanf(“%d”,&num);
for(i = 0; i < num;i++)
{
if(i == 0)
{
EnQueue(&sq,0);
EnQueue(&sq,1);
EnQueue(&sq,0);
printf(“1”);
}
else
{
EnQueue(&qs,0);
while(QueueLength(sq) != 1)
{
t=DelQueue(&sq)+GetFront(sq);
EnQueue(&qs,t);
}
DelQueue(&sq);
EnQueue(&qs,0);
length = QueueLength(qs);
for( j = 0; j < length; j++)
{
ret = DelQueue(&qs);
if(ret != 0)
{
printf(“%d “,ret);
}
EnQueue(&sq ,ret);
}
}
printf(“\n”);
}
return 0;
}