欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

队列的链式实现

程序员文章站 2022-07-14 14:01:16
...

用链式存储结构,实现教材定义的队列的基本操作。
菜单中包括以下功能:
1.初始化队列,2.销毁队列,3.清空队列,4. 队列判空,5.求队列长度,6.获取队头元素,7.插入一个 元素,8.删除一个元素,9输出所有元素。
要求:自定义的函数中不允许出现提示语和输出语句。

#include <iostream>
using namespace std;


typedef struct Code
{
	struct Code* next;
	int data;
}Code,* List;

typedef struct
{
	Code *front;
	Code *end;
	int length;
}str;

//初始化一个队列
str InistList(str &L)
{
	L.front = L.end = (List)malloc(sizeof(Code));
	if (L.front == NULL)
		exit(OVERFLOW);
	else
	{
		L.front->next = NULL;
		L.length = 0;
	}
	return L;
}

//销毁一个队列
int ListDistory(str &L)
{
	List p = L.front->next;
	free(L.front);
	L.front = NULL;
	L.end = NULL;
	while (true)
	{
		if (p != NULL)
		{
			List q = p;
			p = p->next;
			free(q);
			q = NULL;
		}
		else
			break;
	}
	return true;
}

//清空一个队列
int ClearList(str &L)
{
	List p = L.front->next;
	L.front->data = NULL;
	L.front->next = NULL;
	L.end = L.front;
	L.length = 0;
	while (true)
	{
		if (p != NULL)
		{
			List q = p;
			p = p->next;
			free(q);
			q = NULL;
		}
		else
			break;
	}
	return true;
}

//队列判空
int ListEmpty(str L)
{
	return L.length;
}

//求队列长度
int ListLength(str L)
{
	return L.length;
}

//获取队头元素
int GetHead(str L)
{
	return L.front->data;
}

//插入一个元素
int InsertElem(str& L, int elem)
{
	if (L.length == 0)
	{
		L.front->data = elem;
		L.length++;
		return true;
	}
	List OneElem = NULL;
	OneElem = (List)malloc(sizeof(Code));
	if (OneElem == NULL)
		exit(OVERFLOW);
	else
	{
		OneElem->next = NULL;
		OneElem->data = elem;
		L.end->next = OneElem;
		L.end = OneElem;
		L.length++;
	}
	return true;
}

//删除一个元素
int DeletElem(str &L)
{
	if (L.length == 1)
	{
		int elem = L.front->data;
		L.front->data = NULL;
		L.length--;
		return elem;
	}
	List p = L.front;
	int elem = L.front->data;
	L.front = L.front->next;
	free(p);
	p = NULL;
	L.length--;
	return elem;
}

//输出所有元素
int ElemPrint(str L)
{
	List p = L.front;
	for (int i = 0; i < L.length; i++)
	{
		cout << p->data << " ";
		p = p->next;
	}
	cout << endl;
	return true;
}

int main()
{
	int a,i;
	int count = 0;
	str L;
	while (true)
	{
		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;
		cin >> a;
		if (a < 0)
		{
			cout << "已退出" << endl;
			break;
		}
		else if (a > 9 || a == 0)
		{
			cout << "输入错误,请重新输入" << endl;
			continue;
		}
		switch (a)
		{
		case 1:
		{
			if (count == 0)
			{
				L = InistList(L);
				cout << "已成功初始化一个队列" << endl;
				count = 1;
			}
			else
				cout << "队列已存在,请先销毁该链表" << endl;
			break;
		}
		case 2:
		{
			if (count == 1)
			{
				ListDistory(L);
				cout << "已成功销毁一个队列" << endl;
				count = 0;
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 3:
		{
			if (count == 1)
			{
				ClearList(L);
				cout << "队列已清空" << endl;
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 4:
		{
			if (count == 1)
			{
				i = ListEmpty(L);
				if (i == 0)
					cout << "当前队列为空队列" << endl;
				else
					cout << "当前队列不为空" << endl;
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 5:
		{
			if (count == 1)
			{
				i = ListLength(L);
				cout << "当前队列长度为" << i << endl;
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 6:
		{
			if (count == 1)
			{
				if (L.length == 0)
				{
					cout << "当前队列为空队列,无队头元素" << endl;
				}
				else
				{
					i = GetHead(L);
					cout << "当前队列队头元素为" << i << endl;
				}
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 7:
		{
			if (count == 1)
			{
				cout << "请输入要插入的元素" << endl;
				cin >> i;
				InsertElem(L, i);
				cout << "插入" << i << "后的队列为:";
				ElemPrint(L);
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 8:
		{
			if (count == 1)
			{
				if (L.length == 0)
				{
					cout << "该队列为空队列,无法删除元素" << endl;
				}
				else
				{
					i = DeletElem(L);
					cout << "删除" << i << "后的队列为:";
					if (L.length != 0)
						ElemPrint(L);
					else
						cout << "空队列" << endl;
				}
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		case 9:
		{
			if (count == 1)
			{
				if (L.length == 0)
				{
					cout << "该队列为空队列,不能输出元素" << endl;
				}
				else
				{
					cout << "该队列的元素为:" ;
					ElemPrint(L);
				}
			}
			else
			{
				cout << "队列不存在,请先初始化一个队列" << endl;
			}
			break;
		}
		}
		system("pause");
		system("cls");
	}
}

相关标签: 队列 c++