2021-09-08

数据结构学习(C语言)-单链队列

单链队列

通过单链表的形式实现“先进先出”的队列存储结构,先进队列的元素先出,即要出队(删除)只能从此时队列中最先进入的那个元素开始,同样进队(添加元素)新进入的元素必须排在此时队列中的最后一个元素的后面。

单链队列操作实例及代码
代码:

#include<stdio.h>
#include<stdlib.h>
typedef struct NODE {
	int data;
	struct NODE * pNext;
}node, *pNode;
typedef struct QUEUE {
	pNode front;
	pNode rear;
}queue, *pQueue;
//函数声明
void initQueue(pQueue);//初始化一个空队
void intoQueue(pQueue, int);//进队
void traverse(pQueue);//遍历队中所有元素
bool isEmpty(pQueue);//判断是否为空队
void outQueue(pQueue,int *);//出队
void getNum(pQueue);//获取队长(队中元素个数)
void clearQueue(pQueue);//清空队列
//main()函数
int main(void) {
	queue Q;
	int val;
	initQueue(&Q);//初始化一个空队
	intoQueue(&Q, 1);//进队(队尾入队)
	intoQueue(&Q, 2);
	intoQueue(&Q, 3);
	intoQueue(&Q, 4);
	intoQueue(&Q, 5);
	intoQueue(&Q, 6);
	traverse(&Q);//遍历队中所有元素
	outQueue(&Q,&val);//出队(队首离队)
	traverse(&Q);//遍历(出队结果验证)
	getNum(&Q);//队长度
	clearQueue(&Q);//清空队列
	if (isEmpty(&Q)) {//清空验证1
		printf_s("队已清空!\n");
	}
	getNum(&Q); 
	return 0;
}
void initQueue(pQueue pQ) {//初始化(相当于在队首创建了一个无实际意义的头结点)
	pQ->front = (pNode)malloc(sizeof(node));
	pQ->rear = pQ->front;
	if (pQ->front == NULL) {
		printf_s("动态分配空间失败!\n");
		exit(-1);
	}
	pQ->front->pNext = NULL;
	return;
}
void intoQueue(pQueue pQ, int val) {//进队
	pNode pNew = (pNode)malloc(sizeof(node));
	if (pNew == NULL) {
		printf_s("动态分配空间失败!\n");
		exit(-1);
	}
	pNew->data = val;
	pQ->rear->pNext = pNew;
	pQ->rear = pNew;
	pNew->pNext = NULL;
	return;
}
bool isEmpty(pQueue pQ) {//是否为空
	if (pQ->front == pQ->rear)
		return true;
	else
		return false;
}
void traverse(pQueue pQ) {//遍历
	if (isEmpty(pQ)) {
		printf_s("队是空的!\n");
		exit(-1);
	}
	else {
		pNode p = pQ->front->pNext;
		while (p != pQ->rear) {
			printf_s("%d	", p->data);
			p = p->pNext;
		}
		printf_s("%d	", p->data);
	}
	printf_s("\n");
}
void outQueue(pQueue pQ,int * val) {//出队
	if (isEmpty(pQ)) {
		printf_s("队已空,无法再出队!\n");
		exit(-1);
	}
	pNode p = pQ->front->pNext;
	pQ->front->pNext = p->pNext;
	*val = p->data;
	free(p);
	p->pNext = NULL;
	printf_s("队首元素:%d  已出队\n",*val);
	return;
}
void getNum(pQueue pQ) {//队长度
	int len = 0;
	if (isEmpty(pQ)) {
		printf_s("队为空,队长为:0\n");
		exit(-1);
	}
	else {
		pNode p = pQ->front->pNext;
		while (p != pQ->rear) {
			len++;
			p = p->pNext;
		}
		len++;
	}
	printf_s("队长度为:%d\n",len);
}
void clearQueue(pQueue pQ) {//清空队列
	if (isEmpty(pQ)) {
		printf_s("队已空,无需再清空!\n");
		exit(-1);
	}
	else {
		pNode p = pQ->front->pNext;
		pNode q = NULL;
		while (p== pQ->rear) {
			pQ->front->pNext = p->pNext;
			q = p->pNext;
			free(p);
			p->pNext = NULL;
			p = q;
		}
		pQ->front->pNext = NULL;
		pQ->front = pQ->rear;
	}
	return;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Evekkan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值