数据结构》严奶奶版本---队列(1) 链队列 完整源码


数据结构》队列(1) 链队列 完整源码


在这里插入图片描述

//链队列

#include <iostream>
#include <windows.h>
#include <string>
#include "main.h"

using namespace std;

typedef char ElemType;

typedef struct qnode{

	ElemType data;
	struct qnode *next;

}QueueNode,*QNode;

typedef struct linkqueue{
	
	QNode front;
	QNode rear;

}LinkQueue;

bool init_queue(LinkQueue &q);//初始化
bool en_queue(LinkQueue &q,ElemType data);//入队列
bool de_queue(LinkQueue &q,ElemType &data);//出队列
bool clear_queue(LinkQueue &q);//清空队列
bool judge_empty(LinkQueue q);//判空
bool get_length(LinkQueue q,int &length);//队列长度
bool get_head(LinkQueue q,ElemType &data);//得到队头元素
bool traverse_queue(LinkQueue q);//遍历队列
bool judge_hw();//回文

void show();
void switch_channel(int channel);

LinkQueue q;
ElemType data;
int n,i;

int main()
{

	int channel;
	do{
		
		show();
		cin>>channel;
		switch_channel(channel);
		cout<<endl;

	}while(1);

	return 0;
}

void show()
{
	cout<<"链队列操作"<<endl;
	cout<<"1--初始化、入队元素"<<endl;
	cout<<"2--特定位置元素出队"<<endl;
	cout<<"3--输出队头元素"<<endl;
	cout<<"4--队列长度"<<endl;
	cout<<"5--清空栈"<<endl;
	cout<<"6--回文"<<endl;
	
}
void switch_channel(int channel)
{
	switch(channel)
	{
	case 1:{
		   		init_queue(q);//初始化
				cout<<"请输入要入队列元素的数目:";
				cin>>n;
				for(i = 0; i < n; i++)
				{	
					cout<<"请输入第"<<i+1<<"个元素:";
					cin>>data;
					en_queue(q,data);//入队列
				}
				traverse_queue(q);//遍历队列

		   }break;
	case 2:{
		   		de_queue(q,data);//出队列
				cout<<"出队列元素为:"<<data<<endl;
				traverse_queue(q);
		   }break;
	case 3:{		   
		   		get_head(q,data);//得到队头元素
				cout<<"列头元素为:"<<data<<endl;
				traverse_queue(q);
		   }break;
	case 4:{
		   		get_length(q,n);//队列长度
				cout<<"当前队列长度为:"<<n<<endl;
		   }break;
	case 5:{
		   		clear_queue(q);//清空队列
				traverse_queue(q);
		   }break;
	case 6:{
		   		judge_hw();//回文
		   }break;

	}
	
}

//初始化
bool init_queue(LinkQueue &q)
{
	q.front = q.rear = new QueueNode;
	
	if(!q.front)
		return false;

	q.front->next = NULL;

	cout<<"初始化完成\n";

	return true;
}

//入队列
bool en_queue(LinkQueue &q,ElemType data)
{
	QNode p = new QueueNode;
	p->data = data;
	q.rear->next = p;
	p->next = NULL;
	q.rear = p;
	
	return true;

}

//出队列
bool de_queue(LinkQueue &q,ElemType &data)
{
	if(judge_empty(q))
	{
		cout<<"队列为空"<<endl;
		return false;
	}
	
	QNode p;
	p = q.front;
	q.front = q.front->next;
	data = q.front->data;

	delete p;

	return true;

}

//清空队列
bool clear_queue(LinkQueue &q)
{
	QNode p;
	while(q.front!=q.rear)
	{
		p = q.front;
		q.front = q.front->next;
		delete p;
	}
	cout<<"清空完成"<<endl;
	return true;
}

//判空
bool judge_empty(LinkQueue q)
{
	if(q.front == q.rear)
		return true;

	return false;
}

//队列长度
bool get_length(LinkQueue q,int &length)
{
	length = 0;

	while(q.front->next)
	{
		length++;
		q.front = q.front->next;
	}

	return true;

}

//得到队头元素
bool get_head(LinkQueue q,ElemType &data)
{
	if(judge_empty(q))
	{
		cout<<"队列为空"<<endl;
		return false;
	}
	
	data = q.front->next->data;

	return true;

}
//遍历队列
bool traverse_queue(LinkQueue q)
{
	if(judge_empty(q))
	{
		cout<<"遍历队列--队列为空"<<endl;
		return false;
	}

	cout<<"遍历队列:";

	while(q.front->next)
	{
		cout<<q.front->next->data<<" ";
		q.front = q.front->next;
		
	}

	cout<<endl;

	return true;

}

bool judge_hw()//回文
{
	LinkQueue lq;
	Sqstack sq;
	
	init_queue(lq);
	init_stack(sq);
	
	string s;
	int i;
	SElemType c1,c2;

	cout<<"请输入需要判断是否为回文数的值:";
	cin>>s;

	for(i=0;i<s.length();i++)
	{
		push(sq,s[i]);
		en_queue(lq,s[i]);	
	}

	while(!judge_empty(lq))
	{
		pop(sq,c1);
		de_queue(lq,c2);
		if(c1!=c2)
		{
			cout<<"不是回文数\n";
			return false;
		}
	
	}

	cout<<"是回文数\n";
	return true;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值