【c++回顾】1.3数据类型-栈

实现一个栈,用于存储货物信息(名称,价格,质量)。
栈可以用链表+特殊数据出入方式实现。

实现的功能:

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

代码如下:

//实现一个栈,用于存储货物信息(名称,价格,质量)
//栈可以用链表+特殊数据出入方式实现

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

//定义货物信息结构体
struct Node
{
	char name[20 * 2 + 1];//最多20个汉字
	float price;
	float weight;
	Node* nextnode;
};
//打印节点
void PrintNode(Node node)
{
	cout << node.name << ' ' << node.price << "元 " << node.weight << "kg " << endl;
}

//复制节点数据
void CopyData(Node* object, Node source)
{
	strcpy_s(object->name, source.name);
	object->price = source.price;
	object->weight = source.weight;
}

//定义栈类
class LinkedList
{
public:
	LinkedList();//栈初始化
	~LinkedList();//栈的销毁
	Node* GetPoint(int i);//获取第i个节点的地址
	Node GetNode(int i);//获取第i个节点
	void PushNode(Node node);//入栈操作
	void SearchNode(char* keyword);//查找函数
	Node PopNode();//出栈操作
	void PrintList();//打印栈

private:
	int length;//栈长度
	Node* headpoint;//栈底地址
	Node* endpoint;//栈顶地址
};


//栈初始化
LinkedList::LinkedList()
{
	headpoint = NULL;
	endpoint = NULL;
	length = 0;
	cout << "【栈初始化完成】" << endl;
}

//栈销毁
LinkedList::~LinkedList()
{
	//从后向前逐个释放内存
	for (int i = length - 1; i > -1; i--)
	{
		Node* point = GetPoint(i);
		delete point;
	}
	cout << "【栈已销毁】" << endl;
}

//获取第i个节点的地址
Node* LinkedList::GetPoint(int i)
{
	//如果i不合理,输出警告并返回NULL
	if (i < 0 || i >= length) {
		cout << "【警告】超出栈范围" << endl;
		return NULL;
	}
	//获取第i个节点的地址并返回
	int o;
	Node* point = headpoint;
	for (o = 0; o < i; o++)
	{
		point = point->nextnode;
	}
	return point;
}

//获取第i个节点
Node LinkedList::GetNode(int i)
{
	//如果i不合理,输出警告并返回NULL
	if (i < 0 || i >= length) {
		cout << "【警告】超出栈范围,已返回头节点" << endl;
		return *headpoint;
	}
	//获取第i个节点
	Node* point = GetPoint(i);
	return *point;
}

//入栈
void LinkedList::PushNode(Node node)
{
	//如果栈为空
	if (length == 0)
	{
		//申请内存
		Node* point = new Node;
		//复制数据
		CopyData(point, node);
		//设置下一节点的指针
		point->nextnode = NULL;
		//链接
		headpoint = point;
		endpoint = point;
	}
	//否则
	else
	{
		//申明一个node型数据并赋值
		Node* nodepoint = new Node;
		nodepoint->nextnode = NULL;
		CopyData(nodepoint, node);
		//链接
		endpoint->nextnode = nodepoint;
		//修改栈顶地址
		endpoint = nodepoint;
	}
	//修改栈长度
	length++;
}

//出栈
Node LinkedList::PopNode()
{
	//检验栈是否为空,如果为空,则报错
	if (length==0)
	{
		cout << "【警告】栈已空" << endl;
		Node node = { "error",0,0,NULL };
		return node;
	}
	//记录栈顶节点的信息
	Node* topnodepoint = new Node;
	topnodepoint->nextnode = NULL;
	CopyData(topnodepoint, *endpoint);
	//删除节点
	delete endpoint;
	//修改栈长
	length--;
	//修改栈顶地址
	endpoint = GetPoint(length - 1);
	//返回数据
	return *topnodepoint;
}

//查找包含keyword关键字的节点,并输出节点序号及节点内容
void LinkedList::SearchNode(char* keyword)
{
	//利用string库实现是否包含关键词的判断
	Node* point = headpoint;
	string key = keyword;
	//遍历整个栈
	for (int i = 0; i < length; i++)
	{
		//类型转换
		string name = point->name;
		string price = to_string(point->price);
		string weight = to_string(point->weight);
		bool flag = false;
		//三者中只要有一个找到关键字,就打印该数据及其序号
		if (price.find(key) != string::npos || name.find(key) != string::npos || weight.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.price >> node.weight;
	node.nextnode = NULL;
	return node;
}

//打印整个栈
void LinkedList::PrintList()
{
	cout << "【打印栈】" << endl;
	int i;
	for (i = 0; i < length; i++)
	{
		PrintNode(GetNode(i));
	}
	cout << "【打印结束】" << endl;
}


//主函数测试各功能
int main()
{
	//初始化
	LinkedList linkedlist;
	//入栈n个节点
	int n;
	cout << "请输入货物数量:" << endl;
	cin >> n;
	//检验n是否合理
	if (n < 0)cout << "【警告】输入有误" << endl;
	for (int i = 0; i < n; i++)
	{
		linkedlist.PushNode(InputNode());
	}
	//打印整个栈
	linkedlist.PrintList();

	//出栈
	Node node = linkedlist.PopNode();
	cout << "出栈节点:";
	PrintNode(node);
	//打印整个栈
	linkedlist.PrintList();

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

	return 0;
}




示例结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值