数据结构实验——链队列

实验题目:用链式存储结构实现队列的基本操作

1.初始化队列,2.销毁队列,3.清空队列,4. 队列判空,5.求队列长度,6.获取队头元素,7.插入一个元素,8.删除一个元素,9.输出所有元素。

要求:自定义的函数中不允许出现提示语和输出语句。


链队列:使用链表实现的队列;具有队头指针和队尾指针,指示队列元素所在的位置。

特性:

· 在队尾插入元素,在队头删除元素

· 满足队列特点先进先出

时间复杂度:

读取、插入、删除时,时间复杂度均为O(1)

 


实现代码:

#include <bits/stdc++.h>
using namespace std;

//定义声明区
#define OK 1
#define ERROR 0
typedef int QElemType;
typedef int Status; 
bool isInit = false; 

//单链队列存储结构 
typedef struct QNode {
	QElemType data;
	struct QNode *next;
}QNode, *QueuePtr;

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

//函数声明区
void menu(); //功能菜单 
Status InitQueue (LinkQueue &Q); //初始化队列 
Status DestoryQueue (LinkQueue &Q); //销毁队列 
Status ClearQueue (LinkQueue &Q); //清空队列 
Status QueueEmpty (LinkQueue Q); //队列判空 
Status QueueLength (LinkQueue Q, int &length); //求队列长度 
Status EnQueue (LinkQueue &Q, QElemType e); //插入一个元素
Status GetHead(LinkQueue Q, QElemType &e); //获取队头元素 
Status DeQueue (LinkQueue &Q, QElemType &e); //删除一个元素 
Status OutputQueue (LinkQueue Q, QElemType *arr, int length); //输出所有元素 

//主函数 
int main (){
	LinkQueue Q; 
	int option = 0;
	int length = 0; //队列长度 
	QElemType e; //保存队列元素 
	int count = 0; //需要插入的元素个数 
	int arr_length; //数组长度 
	QElemType arr[1000];
	menu(); 
	while (option >= 0){
		cout << "请输入你的选择:" << endl;
		cin >> option; 
		if (option != 1 && isInit == false && option >= 0){
			cout << "队列未进行初始化,请先初始化队列" << endl;
			continue;
		}
		switch (option){
			case 1:
				InitQueue(Q);
				cout << "初始化成功" << endl; 
				break;
			case 2:
				DestoryQueue(Q);
				cout << "销毁成功" << endl; 
				break;
			case 3:
				ClearQueue(Q);
				cout << "清空成功" << endl; 
				break;
			case 4:
				if (QueueEmpty(Q)){
					cout << "该队列为空" << endl;
				} else {
					cout << "该队列不为空" << endl; 
				}
				break;
			case 5:
				QueueLength(Q, length);
				cout << "队列的长度为:" << length << endl;
				break;
			case 6:
				GetHead(Q, e);
				cout << "队头元素为:" << e << endl; 
				break;
			case 7:
				cout << "请输入你要插入的元素个数:" << endl; 
				cin >> count;
				cout << "请输入你要插入的" << count << "个元素:" << endl;
				for (int i = 0; i < count; ++i){
					cin >> e;
					EnQueue(Q,e); 
				}
				cout << "插入成功" << endl; 
				break;
			case 8:
				DeQueue(Q, e);
				cout << "成功删除队头元素" << e << endl; 
				break;
			case 9:
				QueueLength(Q, length);
				OutputQueue(Q, arr, length);
				for (int i = 0; i < length; ++i){
					cout << arr[i] << "  "; 
				}
				cout << endl;
				break;
			default:
				cout << "成功退出,欢迎下次使用" << endl;
				break; 
		}
	}
}


//菜单 
void menu(){
	cout << "~~~~~~Spraing※boy~~~~~~" << endl;
	cout << "**    1.初始化队列    **" << endl;
	cout << "**    2.销毁队列      **" << endl;
	cout << "**    3.清空队列      **" << endl;
	cout << "**    4.队列判空      **" << endl;
	cout << "**    5.求队列长度    **" << endl;
	cout << "**    6.获取队头元素  **" << endl;
	cout << "**    7.插入一个元素  **" << endl;
	cout << "**    8.删除一个元素  **" << endl;
	cout << "**    9.输出所有元素  **" << endl;
	cout << "** -> 退出输入负数    **" << endl;
	cout << "************************" << endl; 
}

//初始化队列
Status InitQueue (LinkQueue &Q){
	//构造一个带头结点的空队列
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
	if (!Q.front) exit (OVERFLOW);  //存储分配失败
	Q.front->next = NULL;
	isInit = true; //使初始化标记变为true 
	return OK; 
} 

//销毁队列
Status DestoryQueue (LinkQueue &Q){
	while (Q.front){
		Q.rear = Q.front->next;
		free(Q.front);
		Q.front = Q.rear;
	} 
	isInit = false; //销毁之后使初始化标记变为false 
	return OK;
} 

//清空队列
Status ClearQueue (LinkQueue &Q) {
	//先将队尾指针指向队头指针的下一个结点 
	Q.rear = Q.front->next; 
	while (Q.front->next){
		//队尾指针后移 
		Q.rear = Q.rear->next;
		//释放队头指针和队尾指针中间的结点 
		free(Q.front->next);
		Q.front->next = Q.rear; 
	} 
	//此时对列已为为空,调整队尾指针位置,令它指向队头 
	Q.rear = Q.front;
	return OK;
}

//队列判空
Status QueueEmpty (LinkQueue Q){
	//判空条件为 Q.front == Q.rear 
	if (Q.front == Q.rear){
		return OK;
	} else {
		return ERROR;
	}
} 

//求队列长度
Status QueueLength (LinkQueue Q, int &length) {
	//不断让 Q.front 向后移直到 Q.front == Q.rear
	if (Q.front == Q.rear){
		length = 0;
	}
	while (Q.front != Q.rear){
		length++;
		Q.front = Q.front->next;
	} 
	return OK;
} 


//获取队头元素
Status GetHead(LinkQueue Q, QElemType &e){
	//先判断是否为空 
	if (Q.front == Q.rear) return ERROR;
	e = Q.front->next->data;
	return OK;
} 
//插入一个元素
Status EnQueue (LinkQueue &Q, QElemType e) {
	//为元素分配空间 
	QueuePtr p;
	p = (QueuePtr)malloc(sizeof(QNode));
	if (!p) return ERROR; //分配失败
	p->data = e;
	p->next = NULL;
	Q.rear->next = p;
	Q.rear = p; //新的队尾 
	return OK; 
}

//删除一个元素,队列的原则是先进先出,区别于栈的先进后出 
Status DeQueue (LinkQueue &Q, QElemType &e){
	QueuePtr p;
	if (Q.front == Q.rear) return ERROR; //首先判断队列是否为空
	p = Q.front->next;
	e = p->data; 
	Q.front->next = p->next; //修改队头指针,即删除队头元素的结点
	if (Q.rear == p) Q.rear = Q.front; //特殊情况,队列中只有一个元素
	free (p);
	return OK; 
} 

//输出所有元素
Status OutputQueue (LinkQueue Q, QElemType *arr, int length){
	QueuePtr p;
	int i = 0;
	if (Q.front == Q.rear) return ERROR; //队列为空
	p = Q.front->next;
	while (p != NULL)
	{
		arr[i] = p->data; //用数组来储存队列元素 
		p = p->next;
		++i;
	 } 
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Spraing※boy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值