用队列来编写杨辉三角形

头文件

#ifndef _SEQUENCEQUEUE_H
#define _SEQUENCEQUEUE_H

#include
#include

#define SUCCESS  10000
#define FAILURE  10001
#define TRUE     10002
#define FALSE    10003
#define SIZE     10
struct queue
{
	int data[SIZE];
	int front;
	int rear;
};
typedef struct queue Queue;
int InitQueue(Queue *q);
int EmptyQueue(Queue q);
int EnterQueue(Queue *q,int e);
int Getfirst(Queue q);
int LengthQueue(Queue q);
int DelectQueue(Queue *q);
int DelectQueue(Queue *q);

#endif

 

主函数

#include "SequenceQueue.h"
#include 

int main()
{
	int n;
	scanf("%d",&n);
	
	Queue queue;
	InitQueue(&queue);
	EnterQueue(&queue,0);
	EnterQueue(&queue,1);
	printf("%d\n",1);

	int i,j;
	for(i = 1; i < n; i++)
	{
		EnterQueue(&queue,0);
		for(j = 0; j <= i; j++)
		{
			int s = DelectQueue(&queue);
			s += Getfirst(queue);
			EnterQueue(&queue,s);
			printf("%d ",s);
		}
		printf("\n");
	}
	return 0;
}

 

自定义函数

#include "SequenceQueue.h"

int InitQueue(Queue *q)
{
	if(NULL == q)
	{
		return FAILURE;
	}

	q->rear = q->front = 0;

	return SUCCESS;

}

int EmptyQueue(Queue q)
{
	return (q.front == q.rear) ? TRUE : FALSE;
}

int EnterQueue(Queue *q,int e)
{	
	if(NULL == q)
	{
		return FAILURE;
	}

	if((q->rear + 1) % SIZE == q->front)
	{
		return FAILURE;
	}

	q->data[q->rear] = e;
	q->rear = (q->rear + 1) % SIZE;

	return SUCCESS;

}

int Getfirst(Queue q)
{
	if(q.rear == q.front)
	{
		return FAILURE;
	}

	return q.data[q.front];
}

int LengthQueue(Queue q)
{
	return (q.rear - q.front + SIZE) % SIZE;
}

int DelectQueue(Queue *q)
{
	if(NULL == q)
	{
		return FAILURE;
	}
	if(q->rear == q->front)
	{
		return FAILURE;
	}
	
	int e = q->data[q->front];
	q->front = (q->front + 1) % SIZE;

	return e;
}

int ClearQueue(Queue *q)
{
	if(NULL == q)
	{
		return FAILURE;
	}

	q->front = q->rear;

	return SUCCESS;
}

 

你可能感兴趣的:(用队列来编写杨辉三角形)