循环队列,链队列的实现,打印杨辉三角

循环队列的C语言实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
typedef int ElemType;
typedef struct QueueSq
{
     
	ElemType * queue;  //指向存储队列的存储空间
	int front, rear;   //队手指针 队尾指针
	int Maxsize;       //queue数组长度
}QueueSq;

//扩展存储空间函数
void againMalloc(QueueSq * Q)
{
     
	ElemType *p;
	p = realloc(Q->queue, 2 * Q->Maxsize * sizeof(ElemType));//重新开辟内存为最大内存大的两倍
	if (!p)
	{
     
		printf("存储空间已用完\n");
		exit(1);
	}
	Q->queue = p;  //使queue指向新的队列空间
	if (Q->rear != Q->Maxsize - 1)
	{
     
		for (int i = 0; i < Q->rear; i++)
		{
     
			Q->queue[i + Q->Maxsize] = Q->queue[i]; //原队列的内容向后移动Maxsize
			Q->rear += Q->Maxsize;                  //队尾指针后移Maxsize
		}
	}
	Q->Maxsize = 2 * Q->Maxsize;      //队列空间修改为原来的2倍
	printf("存储空间已为当前两倍\n");
}

//初始化队列
void InitQueue(QueueSq * Q, int ms)
{
     
	if (ms <= 0)
	{
     
		printf("ms值非法!\n");
		exit(1);//程序异常退出
	}
	Q->Maxsize = ms;
	Q->queue = malloc(ms * sizeof(ElemType));
	if (!Q->queue)
	{
     
		printf("内存空间已用完\n");
		exit(1);
	}
	Q->front = Q->rear = 0;//置队列为空
}

//向队列插入元素
void EnQueue(QueueSq * Q, ElemType x)
{
     
	if ((Q->rear + 1) % Q->Maxsize == Q->front) //队空间满
	{
     
		againMalloc(Q);
	}
	Q->queue[Q->rear] = x;
	Q->rear = (Q->rear + 1) % Q->Maxsize;
}

//从队列删除元素并返回
ElemType OutQueue(QueueSq *Q)
{
     
	ElemType temp;
	if (Q->front == Q->rear) //队列为空
	{
     
		exit(1);
	}
	temp = Q->queue[Q->front];//保留删除的数据
	Q->front = (Q->front + 1) % Q->Maxsize;
	return temp;
}

//读取队首元素,不改变队列状态
ElemType PeekQueue(QueueSq * Q)
{
     
	if (Q->front == Q->rear) //队列为空
	{
     
		exit(1);
	}
	return Q->queue[Q->front];
}

//检查一个队列是否为空,若是则返回1,否则返回0
int EmptyQueue(QueueSq * Q)
{
     
	if (Q->front == Q->rear)
	{
     
		return 1;
	}
	else
		return 0;
}

//清除一个队列为空,释放动态存储空间
void ClearQueue(QueueSq * Q)
{
     
	if (Q->queue != NULL)
	{
     
		free(Q->queue);
		Q->queue = NULL;
		Q->front = Q->rear = 0;
		Q->Maxsize = 0;
	}
}

//主函数
int main()
{
     
	QueueSq  q;
	int a[8] = {
      3, 8, 5, 17, 9, 30, 15, 22 };
	InitQueue(&q, 5);
	for (int i = 0; i < 8; i++)
	{
     
		EnQueue(&q, a[i]);
	}
		printf("%d ", OutQueue(&q));
		printf("%d\n", OutQueue(&q));
		EnQueue(&q, 68);
		printf("%d ", PeekQueue(&q));
		printf("%d\n", OutQueue(&q));
		while (!EmptyQueue(&q))
		{
     
			printf("%d ", OutQueue(&q));
	    }
		printf("\n");
		ClearQueue(&q);


	return 0;
}

运行截图:
循环队列,链队列的实现,打印杨辉三角_第1张图片
链队列的c语言实现:

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
typedef int QueueElemType;
typedef struct Node
{
     
	QueueElemType data;//数据域
	struct Node * next;//指针域
}LinkQueueNode;

typedef struct
{
     
	LinkQueueNode * front;
	LinkQueueNode * rear;
}LinkQueue;

//链队列的初始化
void InitQueue(LinkQueue * Q)
{
     
	Q->front = (LinkQueueNode *) malloc(sizeof(LinkQueueNode));
	
	if (Q->front)
	{
     
		Q->rear = Q->front;
		Q->front->next = NULL;
	}
}

//链队列入队操作算法
void EnQueue(LinkQueue * Q, QueueElemType x)
{
     
	LinkQueueNode * NewNode;
	NewNode = (LinkQueueNode *)malloc(sizeof(LinkQueueNode));

	if (NewNode)
	{
     
		NewNode->next = NULL;
		NewNode->data = x;
		Q->rear->next = NewNode;
		Q->rear = NewNode;
	}
}

//链队列出队操作算法
QueueElemType OutQueue(LinkQueue * Q)
{
     
	LinkQueueNode * p;
	if (Q->front == Q->rear)
		exit(1);
	p = Q->front->next;
	Q->front->next = p->next;//元素p出队
	if (Q->rear == p)  //队中只有一个元素
	{
     
		Q->rear = Q->front;
	}
	QueueElemType temp = p->data;
	free(p);
	return temp;
}

//读取队首元素
QueueElemType PeekQueue(LinkQueue * Q)
{
     
	if (Q->front == Q->rear)
		exit(1);
	return Q->front->next->data;
}

//判断队列是否为空 若为空 返回1 不为空 返回0
int EmptyQueue(LinkQueue * Q)
{
     
	if (Q->front == Q->rear)
	{
     
		return 1;
	}
	else
	{
     
		return 0;
	}
}

//清空队列
void ClearQueue(LinkQueue * Q)
{
     
	LinkQueueNode * p = Q->front->next;
	while (p != NULL)
	{
     
		Q->front = Q->front->next;
		free(p);
	}
	Q->rear = Q->front;
}

int main()
{
     
	LinkQueue q;
	QueueElemType a[8] = {
      3, 8, 5, 17, 9, 30, 15, 22 };
	InitQueue(&q);
	for (int i = 0; i < 8; i++)
	{
     
		EnQueue(&q, a[i]);
	}
	printf("%d ", OutQueue(&q));
	printf("%d \n", OutQueue(&q));
	EnQueue(&q, 68);
	printf("%d ", PeekQueue(&q));
	printf("%d \n", OutQueue(&q));

	while (!EmptyQueue(&q))
	{
     
		printf("%d ", OutQueue(&q));
	}
	printf("\n");
	ClearQueue(&q);
	return 0;
}

利用循环队列打印杨辉三角形算法:

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
typedef int ElemType;
typedef struct QueueSq
{
     
	ElemType * queue;  //指向存储队列的存储空间
	int front, rear;   //队手指针 队尾指针
	int Maxsize;       //queue数组长度
}QueueSq;


//初始化队列
void InitQueue(QueueSq * Q, int ms)
{
     
	if (ms <= 0)
	{
     
		printf("ms值非法!\n");
		exit(1);//程序异常退出
	}
	Q->Maxsize = ms;
	Q->queue = malloc(ms * sizeof(ElemType));
	if (!Q->queue)
	{
     
		printf("内存空间已用完\n");
		exit(1);
	}
	Q->front = Q->rear = 0;//置队列为空
}

//向队列插入元素
void EnQueue(QueueSq * Q, ElemType x)
{
     
	if ((Q->rear + 1) % Q->Maxsize == Q->front) //队空间满
	{
     
		printf("队列已满");
	}
	Q->queue[Q->rear] = x;
	Q->rear = (Q->rear + 1) % Q->Maxsize;
}

//从队列删除元素并返回
ElemType OutQueue(QueueSq *Q)
{
     
	ElemType temp;
	if (Q->front == Q->rear) //队列为空
	{
     
		exit(1);
	}
	temp = Q->queue[Q->front];//保留删除的数据
	Q->front = (Q->front + 1) % Q->Maxsize;
	return temp;
}

//读取队首元素,不改变队列状态
ElemType PeekQueue(QueueSq * Q)
{
     
	if (Q->front == Q->rear) //队列为空
	{
     
		exit(1);
	}
	return Q->queue[Q->front];
}

//检查一个队列是否为空,若是则返回1,否则返回0
int EmptyQueue(QueueSq * Q)
{
     
	if (Q->front == Q->rear)
	{
     
		return 1;
	}
	else
		return 0;
}

void YangHuiTriangle(QueueSq * Q)
{
     
	int line = 0;
	printf("请输入杨辉三角的行数: \n");
	scanf("%d", &line);
	InitQueue(Q,line+2);//初始化队列
	EnQueue(Q, 1);//第一行元素入队
	for (int n = 2; n <= line; n++)//利用队中第n-1行产生第n行的中间n-2个元素
	{
     
		EnQueue(Q, 1);//第n行的第一个元素入队
		for (int j = 1; j <= n - 2; j++)
		{
     
			int temp = OutQueue(Q);
			printf("%d ", temp);//打印n-1行元素
			int qhead = PeekQueue(Q);
			temp = temp + qhead;//利用n-1行产生第n行
			EnQueue(Q, temp);
		}
		printf("%d ", OutQueue(Q));//打印n-1行的最后一个元素
		EnQueue(Q, 1);//第n行的最后一个元素入队
		printf("\n");
	}

		while (!EmptyQueue(Q))
		{
     
			printf("%d ", OutQueue(Q));
		}
}

//主函数
int main()
{
     
	QueueSq  q;
	YangHuiTriangle(&q);

	return 0;
}

远行截图:
循环队列,链队列的实现,打印杨辉三角_第2张图片

你可能感兴趣的:(数据结构,队列,链表,数据结构)