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

2022-03-26

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

2022-03-26

一、创建三个文件

 

二、代码实现 

1.Queue.c部分 

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "Queue.h"



enum Option
{
	退出队列,
	插入数据,
	删除数据,
	获取首元素,
	获取尾元素,
	判断是否为空,
	判断队列大小
};



void menu()
{
	printf("*********************************************n");
	printf("******    1.插入数据     2.删除数据    ******n");
	printf("******    3.获取首元素   4.获取尾元素  ******n");
	printf("******    5.判断是否为空 6.判断队列大小******n");
	printf("******    0.退出队列                   ******n");
	printf("*********************************************n");
}


int main()
{
	pQueue pq;
	QueueInit(&pq);
	int input = 0;
	do
	{
		menu();
		printf("请输入要进行的操作:");
		scanf("%d", &input);
		switch (input)
		{
		case(插入数据):
			QueuePush(&pq);
			break;
		case(删除数据):
			QueuePop(&pq);
			break;
		case(获取首元素):
			QueueFront(&pq);
			break;
		case(获取尾元素):
			QueueBack(&pq);
			break;
		case(判断是否为空):
			QueueEmpty(&pq);
			break;
		case(判断队列大小):
			QueueSize(&pq);
			break;
		case(退出队列):
			QueueDestroy(&pq);
			break;
		default:
			printf("输入有误,请重新输入n");
			break;
		}
	} while (input);
	return 0;
}

2.QueueFunc.c部分 

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


//1.定义结构
typedef struct QueueNode
{
	int data;
	struct QueueNode* next;
}QNode;



//2.定义指向头节点和尾节点的指针
typedef struct Queue
{
	QNode* head;
	QNode* tail;
}pQueue;



//3.初始化队列函数声明
void QueueInit(pQueue* pq);


//4.回收空间函数声明
void QueueDestroy(pQueue* pq);


//5.插入数据函数声明
void QueuePush(pQueue* pq);


//6.出队列函数声明
void QueuePop(pQueue* pq);


//7.判断队列是否为空函数声明
void QueueEmpty(pQueue* pq);


//8.查看队列大小函数声明
size_t QueueSize(pQueue* pq);


//9.获取首元素函数声明
void QueueFront(pQueue* pq);


//10.获取尾元素函数声明
void QueueBack(pQueue* pq);

3.Queue.h部分 

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "Queue.h"




//3.初始化队列函数实现
void QueueInit(pQueue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;
}



//4.回收空间函数实现
void QueueDestroy(pQueue* pq)
{
	assert(pq);
	if (pq->head == NULL)
	{
		return;
	}
	QNode* cur = pq->head;
	QNode* next= cur->next;
	while (cur != NULL)
	{
		next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}



//5.插入数据函数实现
void QueuePush(pQueue* pq)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	printf("请输入要插入的数值:");
	int x = 0;
	scanf("%d", &x);
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL && pq->tail == NULL)
	{
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	printf("插入数据成功!n");
}



//6.出队列函数实现
void QueuePop(pQueue* pq)
{
	assert(pq);
	if (pq->head == NULL)
	{
		printf("队列中无数据n");
		return;
	}
	if (pq->head == pq->tail)
	{
		free(pq->head);
		pq->head = pq->tail= NULL;
	}
	else
	{
		QNode* nextnode = pq->head->next;
		free(pq->head);
		pq->head = nextnode;
	}
	printf("删除数据成功!n");
}



//7.判断队列是否为空函数实现
void QueueEmpty(pQueue* pq)
{
	assert(pq);
	if (pq->head == NULL)
	{
		printf("队列为空n");
	}
	else
	{
		printf("队列不为空n");
	}
}



//8.查看队列大小函数实现
size_t QueueSize(pQueue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	size_t size = 0;
	while (cur)
	{
		size++;
		cur = cur->next;
	}
	printf("%dn", size);
}



//9.获取首元素函数实现
void QueueFront(pQueue* pq)
{
	assert(pq);
	if (pq->head == NULL)
	{
		printf("无数据,无法获取n");
		return;
	}
	else
	{
		printf("%dn", pq->head->data);
	}
}



//10.获取尾元素函数实现
void QueueBack(pQueue* pq)
{
	assert(pq);
	if (pq->tail == NULL)
	{
		printf("无数据,无法获取n");
		return;
	}
	else
	{
		printf("%dn", pq->tail->data);
	}
}

 

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

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

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