栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构:链队的基本操作及实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构:链队的基本操作及实现

一、链队的结构定义
#define ElemType int

typedef struct node{
	ElemType data;
	struct node* next;
}Qnode, *PQnode;

typedef struct {
	PQnode front, rear;
}LinkQueue, *PLinkQueue;
二、基本操作的实现
//初始化队列
PLinkQueue Init_LinkQueue() {
	PLinkQueue Q;
	Q = (PLinkQueue)malloc(sizeof(LinkQueue));
	if (Q) {
		Q->front = NULL;
		Q->rear = NULL;
		printf("初始化成功n");
	}
	return Q;
}

//判空,为空时返回1,不为空返回0
int Empty_LinkQueue(PLinkQueue Q) {
	if (Q && Q->front == NULL && Q->rear == NULL)
		return 1;
	return 0;
}

//入队
void In_LinkQueue(PLinkQueue Q, ElemType e) {
	PQnode x = (PQnode)malloc(sizeof(Qnode));
	if (!x) {
		printf("插入失败,内存不足n");
		return;
	}
	x->data = e;
	x->next = NULL;
	if (Empty_LinkQueue(Q)) {
		Q->front = Q->rear = x;
		printf("入队成功n");
	}
	else {
		Q->rear->next = x;
		Q->rear = x;
		printf("入队成功n");
	}
}

//出队,用e来保存返回元素
void Out_LinkQueue(PLinkQueue Q, ElemType* e) {
	PQnode p;
	if (Empty_LinkQueue(Q)) {
		printf("出队失败,队空n");
		return;
	}
	*e = Q->front->data;
	p = Q->front;
	Q->front = Q->front->next;
	free(p);
	if (!Q->front)
		Q->rear = NULL;
	printf("出队成功,出队元素为:%dn", *e);
}

//读队头元素,用x返回队头元素
int Front_LinkQueue(PLinkQueue Q, ElemType* x) {
	if (Empty_LinkQueue(Q))
		return 0;
	*x = Q->front->data;
	return 1;
}

//销毁队列
void Destroy_LinkQueue(PLinkQueue* Q) {
	PQnode p;
	if (*Q) {
		while ((*Q)->front) {
			p = (*Q)->front;
			(*Q)->front = (*Q)->front->next;
			free(p);
		}
		free(*Q);
	}
	*Q = NULL;
}

//显示所有元素
void Show_LinkQueue(PLinkQueue Q) {
	PLinkQueue p = Q;
	if (Empty_LinkQueue(Q)) {
		printf("队空n");
		return;
	}
	while (p->front) {
		printf("%dn", p->front->data);
		p->front = p->front->next;
	}
}
三、测试代码
#include
#include



int main()
{
	PLinkQueue Q;
	int option = 0;
	int x = 0;
	int i = 0;  //判断是否返回dui
	Q = (PLinkQueue)malloc(sizeof(LinkQueue));
	printf("****************************************n");
	printf("**** 1.初始化        2.判空         ****n");
	printf("**** 3.入队          4.出队         ****n");
	printf("**** 5.读队头元素    6.显示所有元素 ****n");
	printf("**** 7.销毁队列      0.退出         ****n");
	printf("****************************************n");
	do {
		printf("请输入:");
		scanf("%d", &option);
		switch (option) {
			case 1:
				Q = Init_LinkQueue();
				break;
			case 2:
				if (Empty_LinkQueue(Q))
					printf("队空n");
				else
					printf("队不为空n");
				break;
			case 3:
				printf("请输入入队元素:");
				scanf("%d", &x);
				In_LinkQueue(Q, x);
				break;
			case 4:
				Out_LinkQueue(Q, &x);
				break;
			case 5:
				i = Front_LinkQueue(Q, &x);
				if (i)
					printf("队头元素为:%dn", x);
				else
					printf("队空,无队头元素n");
				break;
			case 6:
				Show_LinkQueue(Q);
				break;
			case 7:
				Destroy_LinkQueue(&Q);
				printf("销毁成功n");
				break;
			case 0:
				break;
			default:
				break;
			}
	} while (option);


	return 0;
}
四、测试结果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869905.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号