链式队列基本操作的实现

具体代码如下:

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

#define OK 1
#define ERROR 0

typedef int ElemType, Status;

typedef struct LNode    //链式队列节点
{
	ElemType data;    //每个节点的数据
	struct LNode* next;    //指向下一个节点
}LNode, *QueuePtr;

typedef struct LinkQueue
{
	QueuePtr front;    //队头指针
	QueuePtr rear;    //队尾指针
}LinkQueue;

Status InitQueue(LinkQueue& Q)    //链式队列的初始化
{
	Q.front = (LNode*)malloc(sizeof(LNode));
	if (Q.front == NULL) exit(0);
	Q.front->next = NULL;
	Q.rear = Q.front;
	return OK;
}

Status DestroyQueue(LinkQueue& Q)    //链式队列的销毁
{
	if (Q.front == NULL) return OK;
	LNode* p = Q.front;
	LNode* q;
	while (p)
	{
		q = p;
		p = p->next;
		free(q);
	}
	Q.front = NULL;
	Q.rear = NULL;
	return OK;
}

Status ClearQueue(LinkQueue& Q)    //链式队列的清空
{
	if (Q.front == NULL) return ERROR;
	if (Q.front->next == NULL) return OK;
	LNode* p = Q.front->next;
	LNode* q;
	while (p)
	{
		q = p;
		p = p->next;
		free(q);
	}
	Q.front->next == NULL;
	Q.rear = Q.front;
	return OK;
}

Status QueueEmpty(LinkQueue Q)    //链式队列判空
{
	if (Q.front == Q.rear) return OK;
	return ERROR;
}

Status QueueLength(LinkQueue Q)    //链式队列的长度
{
	if (Q.front == NULL) return ERROR;
	int len = 0;
	LNode* p = Q.front->next;
	while (p)
	{
		len++;
		p = p->next;
	}
	return len;
}

Status GetHead(LinkQueue Q, ElemType& e)    //链式队列获取队头元素
{
	if (Q.front == NULL) return ERROR;
	if (Q.front->next == NULL) return ERROR;
	e = Q.front->next->data;
	return OK;
}

Status QueueTraverse(LinkQueue Q)    //链式队列遍历
{
	if (Q.front == NULL) return ERROR;
	LNode* p = Q.front->next;
	while (p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
	return OK;
}

Status EnQueue(LinkQueue& Q, ElemType e)    //链式队列入队
{
	if (Q.front == NULL) return ERROR;
	LNode* p = (LNode*)malloc(sizeof(LNode));
	if (p == NULL) exit(0);
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p;
	return OK;
}

Status DeQueue(LinkQueue& Q, ElemType& e)    //链式队列出队
{
	if (Q.front == NULL) return ERROR;
	if (Q.front->next == NULL) return ERROR;
	e = Q.front->next->data;
	LNode* p = Q.front->next;
	Q.front->next = p->next;
	if (Q.rear == p)
		Q.rear = Q.front;
	free(p);
	return OK;
}

Status CreateQueue(LinkQueue& Q)    //创建链式队列
{
	InitQueue(Q);
	ElemType e;
	int len;
	printf("队列长度:");
	scanf_s("%d", &len);
	printf("链式队列元素:");
	int i = 0;
	while (i < len)
	{
		scanf_s("%d", &e);
		LNode* p = (LNode*)malloc(sizeof(LNode));
		if (p == NULL) exit(0);
		p->data = e;
		p->next = NULL;
		Q.rear->next = p;
		Q.rear = p;
		i++;
	}
	return OK;
}

int main()
{
	ElemType e;
	LinkQueue Q;
	CreateQueue(Q);
	QueueTraverse(Q);
	e = 0;
	EnQueue(Q, e);
	QueueTraverse(Q);
	GetHead(Q, e);
	printf("队列长度:%d\n", QueueLength(Q));
	printf("队头元素:%d\n", e);
	return OK;
}

运行结果如如下:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值