队列的链式存储结构

队列的链式存储结构

#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;

typedef struct Node {
	ElemType data;
	struct Node* next;
}* QNode;

typedef struct aQueue {
	QNode front;
	QNode rear;
}* Queue;

Queue QueueInit();                                   //创建一个空队列
int QueueEmpty(Queue Q);                             //测试队列Q是否为空
ElemType QueueFirst(Queue Q);                        //返回队列Q的队首元素
ElemType QueueLast(Queue Q);                         //返回队列Q的队尾元素
void EnterQueue(Queue Q, ElemType p);                //入队操作
ElemType DeleteQueue(Queue Q);                       //出队操作

int main()
{
	Queue Q = QueueInit();
	int n, p;
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &p);
		EnterQueue(Q, p);
	}
	printf("%d\n", QueueFirst(Q));
	printf("%d\n", QueueLast(Q));

	for (int i = 0; i < n; i++)
	{
		printf("%d ", DeleteQueue(Q));
	}
	printf("\n");

	return 0;
}

Queue QueueInit()
{
	Queue Q = (Queue)malloc(sizeof(struct aQueue));
	Q->front = Q->rear = NULL;
	return Q;
}

int QueueEmpty(Queue Q)
{
	return Q->front == NULL;
}

ElemType QueueFirst(Queue Q)
{
	if (QueueEmpty(Q))
		return 0;
	return Q->front->data;
}

ElemType QueueLast(Queue Q)
{
	if (QueueEmpty(Q))
		return 0;
	return Q->rear->data;
}

void EnterQueue(Queue Q, ElemType x)
{
	QNode n = (QNode)malloc(sizeof(struct Node));
	n->data = x;
	n->next = NULL;
	if (Q->front)
	{
		Q->rear->next = n;
		Q->rear = n;
	}
	else
	{
		Q->front = Q->rear = n;
	}
}

ElemType DeleteQueue(Queue Q)
{
	if (QueueEmpty(Q))
		return 0;
	ElemType x = Q->front->data;
	QNode p = Q->front;
	Q->front = Q->front->next;
	free(p);
	return x;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值