学习了队列之后我用队列写了一个实现杨辉三角的程序
头文件
#ifndef _SEQUEUE_H_
#define _SEQUEUE_H_
#define MAXSIZE 10
#define SUCCESS 10000
#define FAILURE 10001
typedef int ElemType;
struct sequeue
{
ElemType data[MAXSIZE];
int front;
int rear;
};
typedef struct sequeue SeQueue;
#endif
接口函数
#include <stdio.h>
#include"SeQueue.h"
int QueueInit(SeQueue *Q)
{
Q->front = Q->rear =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 GetQueue(SeQueue Q)
{
return(Q.data[Q.front]);
}
ElemType DelQueue(SeQueue *Q ,ElemType e)
{
if(Q->front == Q->rear)
{
return FAILURE;
}
e = Q->data[Q->front];
Q->front = (Q->front+1)%MAXSIZE;
return e;
}
主函数
#include<stdio.h>
#include"SeQueue.h"
int main()
{
SeQueue a,b;
int i,l,j,ret;
printf("Please input the lines:\n");
scanf("%d",&l);
if(QueueInit(&a)!=SUCCESS || QueueInit(&b)!=SUCCESS)
{
return FAILURE;
}
for(i=0;i<l;i++)
{
if(i==0)
{
EnQueue(&a,0);
EnQueue(&a,1);
EnQueue(&a,0);
printf("1\n");
}
else
{
EnQueue(&b,0);
while(QueueLength(a)!=1)
{
EnQueue(&b,DelQueue(&a)+GetQueue(a));
}
DelQueue(&a);
EnQueue(&b,0);
int length=QueueLength(b);
for(j=0;j<length;j++)
{
ret = DelQueue(&b);
if(ret !=0)
{
printf("%d ",ret);
}
EnQueue(&a,ret);
}
printf("\n");
}
}
return 0;
}