ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【c++回顾】1.4数据类型-队列

2019-08-19 19:42:01  阅读:172  来源: 互联网

标签:1.4 Node cout 队列 数据类型 c++ Queue int length


用类实现一个链式队列,用于存储银行排队信息(姓名,年龄,身份证号)。
类似于栈,队列也可以看作是有着特殊数据出入方式的链表。

实现的功能:

  • 队列的初始化
  • 队列的销毁
  • 获取节点地址
  • 获取节点内容
  • 入队
  • 出队
  • 按关键词查找栈,并返回含有关键词的节点
  • 打印栈

代码如下:

// 使用类实现一个链式队列,用于存储银行排队信息(姓名,年龄,身份证号)

#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

//定义排队信息结构体
struct Node
{
	char name[4 * 2 + 1];
	int age;
	char id[18 + 1];
	Node* nextnode;
};
//打印节点
void PrintNode(Node node)
{
	cout << node.name << ' ' << node.age << ' ' << node.id << endl;
}
//复制节点数据
void CopyData(Node* object, Node source)
{
	strcpy_s(object->name, source.name);
	strcpy_s(object->id, source.id);
	object->age = source.age;
}

//定义队列类
class Queue
{
public:
	Queue();//队列初始化
	~Queue();//销毁队列
	Node* GetPoint(int i);//获取第i个节点的地址
	Node GetNode(int i);//获取第i个节点
	void InQueue(Node node);//入队
	Node OutQueue();//出队
	void PrintQueue();//打印整个队列
	void SearchNode(char* keyword);//查找函数
private:
	int length;//队列长度
	Node* head;//队头
	Node* tail;//队尾
};
//队列初始化
Queue::Queue()
{
	length = 0;
	head = NULL;
	tail = NULL;
	cout << "【队列初始化完成】" << endl;
}
//销毁队列
Queue::~Queue()
{
	//从后向前逐个释放内存
	for (int i = length - 1; i > -1; i--)
	{
		Node* point = GetPoint(i);
		delete point;
	}
	cout << "【队列已销毁】" << endl;
}
//获取第i个节点的地址
Node* Queue::GetPoint(int i)
{
	//如果i不合理,输出警告并返回NULL
	if (i < 0 || i >= length) {
		cout << "【警告】超出队列范围" << endl;
		return NULL;
	}
	//获取第i个节点的地址并返回
	int o;
	Node* point = head;
	for (o = 0; o < i; o++)
	{
		point = point->nextnode;
	}
	return point;
}
//获取第i个节点
Node Queue::GetNode(int i)
{
	//如果i不合理,输出警告并返回NULL
	if (i < 0 || i >= length) {
		cout << "【警告】超出队列范围,已返回头节点" << endl;
		return *head;
	}
	//获取第i个节点
	Node* point = GetPoint(i);
	return *point;
}
//入队
void Queue::InQueue(Node node)
{
	//申请内存
	Node* NewNodePoint = new Node;
	//新节点赋值
	CopyData(NewNodePoint, node);
	NewNodePoint->nextnode = NULL;
	//链接入队列
	  //如果队列为空
	if (length == 0)
	{
		head = NewNodePoint;
		tail = NewNodePoint;
	}
	  //如果队列不为空
	else
	{
		//链接至队尾
		tail->nextnode = NewNodePoint;
		//修改队尾指针
		tail = NewNodePoint;
	}
	//两者均需修改队列长度
	length++;
}
//出队
Node Queue::OutQueue()
{
	//如果队列为空则报错
	if (length == 0)
	{
		cout << "【警告】队列为空,已返回error Node" << endl;
		Node error = { "error",0,"error",NULL };
		return error;
	}
	//不为空则继续
	else
	{
		//记录队列头节点数据
		Node HeadNode;
		CopyData(&HeadNode, *head);
		//修改head指针
		head = head->nextnode;
		//修改队列长度
		length--;
		//返回数据
		return HeadNode;
	}
}
//打印整个队列
void Queue::PrintQueue()
{
	cout << "【打印队列】" << endl;
	int i;
	for (i = 0; i < length; i++)
	{
		PrintNode(GetNode(i));
	}
	cout << "【打印结束】" << endl;
}
//查找包含keyword关键字的节点,并输出节点序号及节点内容
void Queue::SearchNode(char* keyword)
{
	//利用string库实现是否包含关键词的判断
	Node* point = head;
	string key = keyword;
	bool flag = false;
	//遍历整个链表
	for (int i = 0; i < length; i++)
	{
		//类型转换
		string name = point->name;
		string id = point->id;
		string age = to_string(point->age);
		//三者中只要有一个找到关键字,就打印该数据及其序号
		if (age.find(key) != string::npos || name.find(key) != string::npos || id.find(key) != string::npos)
		{
			flag = true;
			cout << i << ' ';
			PrintNode(*point);
		}
		point = point->nextnode;
	}
	//如果没找到含有关键字的数据
	if (!flag)cout << "【查找结束】没有找到匹配项" << endl;
}

//用户输入排队信息
Node InputNode()
{
	cout << "请输入排队信息(姓名,年龄,身份证号):" << endl;
	Node node;
	cin >> node.name >> node.age >> node.id;
	node.nextnode = NULL;
	return node;
}

//主函数测试各功能
int main()
{
	//初始化
	Queue queue;

	//入队测试
	cout << "请输入排队者数量:" << endl;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)queue.InQueue(InputNode());
	//打印队列
	queue.PrintQueue();

	//出队测试
	cout << "请输入出队者数量:" << endl;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cout << "第" << i + 1 << "人:";
		PrintNode(queue.OutQueue());
	}
	//打印队列
	queue.PrintQueue();

	//关键词查找
	char keyword[100];
	cout << "请输入查找关键词:" << endl;
	cin >> keyword;
	queue.SearchNode(keyword);

	return 0;
}


示例结果:
在这里插入图片描述
另外,发现了之前查找函数的bug:忽略了没有找到含有关键词数据的情况,该程序已更正。

标签:1.4,Node,cout,队列,数据类型,c++,Queue,int,length
来源: https://blog.csdn.net/weixin_44340194/article/details/99753007

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有