ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

队列的链式存储结构——链队列——C语言描述

2022-08-13 00:04:26  阅读:134  来源: 互联网

标签:NodeHead 队列 printf LinkQueue Next TraLinkQueue 链式 C语言 Data


队列的链式存储结构——链队列

目录

​ 相当于单链表,入队尾插,出队头删.

1.操作链队列

​ 创建,打印,入队,出队

代码:

void OperateLinkQueue(void) {
	LINK_QUEUE *LinkQueue = (LINK_QUEUE *)malloc(sizeof(LINK_QUEUE));
	LIST_STATUS Status;

	int CreateNum = 3;
	int AddData = 4;
	int *DelData = (int *)malloc(sizeof(int));

	Status = CreateLinkQueue(LinkQueue, CreateNum);
	if (SUCCESS == Status) {
		printf("CreateLinkQueue succeed!\n");
	}
	else {
		printf("CreateLinkQueue failed!\n");
	}
	
	PrintLinkQueue(LinkQueue);

	/*
	Status = EnterLinkQueue(LinkQueue, AddData);
	if (SUCCESS == Status) {
		printf("EnterLinkQueue succeed!\n");
	}
	else {
		printf("EnterLinkQueue failed!\n");
	}
	*/

	Status = ExitLinkQueue(LinkQueue, DelData);
	if (SUCCESS == Status) {
		printf("ExitLinkQueue succeed!\n");
	}
	else {
		printf("ExitLinkQueue failed!\n");
	}

	PrintLinkQueue(LinkQueue);
}

2.创建链队列

​ 创建结点数为3的链队列

代码:

LIST_STATUS CreateLinkQueue(LINK_QUEUE * LinkQueue, int CreateNum) {
	LINK_QUEUE *TraLinkQueue = LinkQueue;
	Node *CreateNode = (Node *)malloc(sizeof(Node));
	int TraIndex = 0;

	printf("CreateLinkQueue start!\n");

	if (NULL == LinkQueue) {
		return ERROR;	
	}

	/*Empty LinkQueue*/
	CreateNode->Data = CreateNum;
	CreateNode->Next = NULL;
	
	TraLinkQueue->Front = CreateNode;
	TraLinkQueue->Rear = CreateNode;

	for (TraIndex = 0; TraIndex < CreateNum - 1; ++TraIndex) {
		CreateNode = (Node *)malloc(sizeof(Node));
		CreateNode->Data = TraIndex;
		CreateNode->Next = NULL;
		
		TraLinkQueue->Rear->Next = CreateNode;
		TraLinkQueue->Rear = TraLinkQueue->Rear->Next;
	}

	printf("CreateLinkQueue end!\n");
	return SUCCESS;
}

结果:

CreateLinkQueue start!

CreateLinkQueue end!

CreateLinkQueue succeed!

PrintLinkQueue start!

TraLinkQueue->Front = 926c48

TraLinkQueue->Rear = 925ec8

NodeHead = 0x926c48, NodeHead->Data = 3, NodeHead->Next = 0x925e90

NodeHead = 0x925e90, NodeHead->Data = 0, NodeHead->Next = 0x925ec8

NodeHead = 0x925ec8, NodeHead->Data = 1, NodeHead->Next = 0x0

PrintLinkQueue end!

3.打印链队列

代码:

LIST_STATUS PrintLinkQueue(LINK_QUEUE * LinkQueue) {
	LINK_QUEUE *TraLinkQueue = LinkQueue;
	Node *NodeHead = LinkQueue->Front;

	printf("PrintLinkQueue start!\n");

	if (NULL == TraLinkQueue) {
		printf("LinkQueue = NULL\n");
		return ERROR;
	}

	printf("TraLinkQueue->Front = %x\n",TraLinkQueue->Front);
	printf("TraLinkQueue->Rear = %x\n", TraLinkQueue->Rear);

	while (NodeHead != NULL) {
		printf("NodeHead = 0x%lx, NodeHead->Data = %d, NodeHead->Next = 0x%lx\n", NodeHead, NodeHead->Data, NodeHead->Next);
		NodeHead = NodeHead->Next;
	}

	printf("PrintLinkQueue end!\n\n");
	return SUCCESS;
}

4.链队列——入队

代码:

LIST_STATUS EnterLinkQueue(LINK_QUEUE * LinkQueue, int AddData) {
	LINK_QUEUE *TraLinkQueue = LinkQueue;
	Node *AddNode = (Node *)malloc(sizeof(Node));

	printf("EnterLinkQueue start!\n");

	if (NULL == TraLinkQueue) {
		return ERROR;
	}

	AddNode->Data = AddData;
	AddNode->Next = NULL;

	TraLinkQueue->Rear->Next = AddNode;
	TraLinkQueue->Rear = AddNode;

	printf("EnterLinkQueue end!\n");
	return SUCCESS;
}

结果:

CreateLinkQueue start!

CreateLinkQueue end!

CreateLinkQueue succeed!

PrintLinkQueue start!

TraLinkQueue->Front = 926c48

TraLinkQueue->Rear = 925ec8

NodeHead = 0x926c48, NodeHead->Data = 3, NodeHead->Next = 0x925e90

NodeHead = 0x925e90, NodeHead->Data = 0, NodeHead->Next = 0x925ec8

NodeHead = 0x925ec8, NodeHead->Data = 1, NodeHead->Next = 0x0

PrintLinkQueue end!

EnterLinkQueue start!

EnterLinkQueue end!

EnterLinkQueue succeed!

PrintLinkQueue start!

TraLinkQueue->Front = 926c48

TraLinkQueue->Rear = 92c3e0

NodeHead = 0x926c48, NodeHead->Data = 3, NodeHead->Next = 0x925e90

NodeHead = 0x925e90, NodeHead->Data = 0, NodeHead->Next = 0x925ec8

NodeHead = 0x925ec8, NodeHead->Data = 1, NodeHead->Next = 0x92c3e0

NodeHead = 0x92c3e0, NodeHead->Data = 4, NodeHead->Next = 0x0

PrintLinkQueue end!

5.链队列——出队

代码:

LIST_STATUS ExitLinkQueue(LINK_QUEUE * LinkQueue, int *DelData) {
	LINK_QUEUE *TraLinkQueue = LinkQueue;
	Node *DelNode;

	printf("ExitLinkQueue start!\n");

	if (NULL == TraLinkQueue) {
		return ERROR;
	}
	
	if (TraLinkQueue->Front == TraLinkQueue->Rear) {
		printf("LinkQueue is empty!\n");
		return ERROR;	
	}

	*DelData = TraLinkQueue->Front->Data;
	DelNode = TraLinkQueue->Front;

	TraLinkQueue->Front = TraLinkQueue->Front->Next;
	free(DelNode);

	printf("ExitLinkQueue end!\n");
	return SUCCESS;
}

结果:

CreateLinkQueue start!

CreateLinkQueue end!

CreateLinkQueue succeed!

PrintLinkQueue start!

TraLinkQueue->Front = 12a5e90

TraLinkQueue->Rear = 12af7f8

NodeHead = 0x12a5e90, NodeHead->Data = 3, NodeHead->Next = 0x12a5ec8

NodeHead = 0x12a5ec8, NodeHead->Data = 0, NodeHead->Next = 0x12af7f8

NodeHead = 0x12af7f8, NodeHead->Data = 1, NodeHead->Next = 0x0

PrintLinkQueue end!

ExitLinkQueue start!

ExitLinkQueue end!

ExitLinkQueue succeed!

PrintLinkQueue start!

TraLinkQueue->Front = 12a5ec8

TraLinkQueue->Rear = 12af7f8

NodeHead = 0x12a5ec8, NodeHead->Data = 0, NodeHead->Next = 0x12af7f8

NodeHead = 0x12af7f8, NodeHead->Data = 1, NodeHead->Next = 0x0

PrintLinkQueue end!

标签:NodeHead,队列,printf,LinkQueue,Next,TraLinkQueue,链式,C语言,Data
来源: https://www.cnblogs.com/meditatorss/p/16581738.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有