队列基本操作,初始化、入队(尾插)、出队(头删)、销毁队、清空队、求队长、遍历队
#include<iostream>
#include<malloc.h>
typedef int elementtype;
using namespace std;
typedef struct snode
{
elementtype data;
struct snode* next;
}linklist,*linkptr;
typedef struct queque
{//定义队列
linkptr head;
linkptr tail;
}queue;
void choose(queue&);//选择操作函数
void initqueue(queue&);//初始化队列头函数
void enqueue(queue&);//插入函数
void outqueue(queue&);//出队函数
void desdroyqueue(queue&);//销毁队列函数
void empty(queue&);//清空队列函数
int queueLength(queue&);//求队长函数
void visitqueue(queue&);//遍历函数
void choose(queue& QL)
{
int a=100;
while (a)
{
cout << "请输入选择:*1.初始化*2.入队*3.出队*4.销毁队*5.清空队*6.求队长*7.遍历队*0.退出\n";
scanf("%d", &a);
//a = (int)getchar() - 48;
cout << "选择值" << a<<'\n';
switch (a)
{
case 0:
{
cout << "成功退出\n";
break;
}
case 1:
{
initqueue(QL);
break;
}
case 2:
{
enqueue(QL);
break;
}
case 3:
{
outqueue(QL);
break;
}
case 4:
desdroyqueue(QL);
break;
case 5:
empty(QL);
break;
case 6:
queueLength(QL);
break;
case 7:
visitqueue(QL);
break;
default:
cout << "输入错误,请重新输入";
break;
}
}
}
void initqueue(queue &QL)
{
QL.head=QL.tail = (linkptr)malloc(sizeof(linklist));
if (QL.head == NULL)
{
exit(OVERFLOW);
}
else
{
QL.head->data = 999;
cout << "头队列创建成功\n";
return;
}
}
void enqueue(queue &QL)//尾插法
{
linkptr tmpptr = QL.head;
int new_queue;
elementtype enqueue_data;
cout << "请输入入队个数:\n";
scanf("%d", &new_queue);
if (new_queue>0 && new_queue<101)
{
cout << "开始执行入队操作,输入值为:" << new_queue << '\n';
for (int i = 1; i <= new_queue; i++)
{
linkptr p = (linkptr)malloc(sizeof(linklist));
if (p == NULL)
{
exit(OVERFLOW);
}
else
{
cout << "请输入入队元素:";
scanf("%d", &enqueue_data);
p->data = enqueue_data;
tmpptr->next = p;
tmpptr = tmpptr->next;
QL.tail = p;
QL.tail->next = NULL;
cout << "入队成功" << i << "\n";
}
}
QL.tail = tmpptr;
QL.tail->next = NULL;
}
else
{
cout << "你输入的有误,请重新开始\n";
}
}
void outqueue(queue &QL)
{
//头出
int i = 0, quelen=0, out_num=0;
linkptr p;
elementtype data;
quelen = queueLength(QL);
cout << "请输入你想出队的个数\n";
scanf("%d", &out_num);
cout << "队列长度:" << quelen<<'\n';
while (out_num< 1 || out_num > quelen)//直到输入合法为止
{
cout << "出队个数不合法:" << out_num << "请重新输入\n";
scanf("%d", &out_num);
}
data = QL.head->next->data;
while(i++<out_num)
{
if (QL.head == QL.tail)
{
printf("已经队列出完,为空队列\n");
break;
}
else
{
data = QL.head->next->data;
cout << "第" << i << "次出队数据为" << data << '\n';
p = QL.head;
QL.head = QL.head->next;
free(p);
}
}
}
void desdroyqueue(queue& QL)//销毁列表
{
while (QL.head)
{
linkptr p = QL.head;
QL.head = QL.head->next;
free(p);
}
if (!QL.head)
{
cout << "销毁队列成功\n";
}
else
{
cout << "销毁队列失败\n";
cout <<"头节点指针" << QL.head<<'\n';
cout << "尾节点指针" << QL.tail<< '\n';
}
}
//清空队
void empty(queue& QL)
{
linkptr p=QL.head;
int i = 0;
int que_len = queueLength(QL);
while (i< que_len)
{
p->data = 0;
p = p->next;
if (p==NULL)
break;
}
if (p == QL.tail)
cout << "清空队列完成\n";
}
//求队长
int queueLength(queue& QL)
{
//cout << "**进入求队长函数\n";
int i = 1;
linkptr p = QL.head;
if (p == NULL)
{
cout << "队长为0\n";
return 0;
}
else
{
while (p != QL.tail)
{
p = p->next;
i++;
if (p == NULL)
break;
}
cout << "通过队长函数求得队长为:" << i << '\n';
return i;
}
}
//遍历队
void visitqueue(queue& QL)
{
linkptr p = QL.head;
int i = 0;
while (p)
{
if (i == 0)
{
cout <<"对头"<<i<<"的元素为:"<< p->data << '\n';
}
else
{
cout << "第" << i << "个队列的元素为:" << p->data << '\n';
}
p = p->next;
i++;
}
cout << "遍历完成\n";
}
int main()
{
queue QL;
int count = 4;
//cout << "请输入选择\n*1.初始化*2.入队*3.出队*4.销毁队*5.清空队*6.求队长*7.遍历队*0.退出\n";
choose(QL);
return 0;
}
一个小菜鸡的数据结构初始路,有不对的地方欢迎指正,我好改正,有什么不清楚的地方也欢迎留言,我会对代码进行说明。