用C++实现队列(以餐厅订单队列为例)

前言

首先,数据结构这个东西并不针对某一种编程语言,但是很多初学者在学习过c语言后先接触的是有关c语言实现的基础数据结构(例如链表,栈,队列等),后面学会了C++之后反而有些束手束脚,用C++的类去实现链栈时反而造成了内存泄漏。

这是因为,我们知道,如果在类内创建对象,那么他在主函数结束后就会自动调用类的析构函数(没写的话会调用默认析构函数),所以有同学就会以为在类中写链式结构时不需要delete或者free,这是错误的想法,因为类的析构只是释放类的内部空间,对于new和malloc所建立的堆区空间仍旧需要调用delete或free。

好的那么废话就不再多说了,我这篇文章已经把源码放到文章末尾了,有需要的直接拿走不谢!当然作者很希望各位看官能给本人点个小赞,谢谢!

Indent:订单类

那么首先是基类订单类,有订单编号account,餐品名称food_name,下一级next,然后我去掉了拷贝构造和移动构造函数,不允许赋值和移动赋值,在基类中添加一个printindent输出函数,输出订单信息。

class Indent {
public:
    int account;
    string food_name;
    Indent* next;
    Indent(int a, string fn, Indent* n = nullptr) :account(a), food_name(fn), next(n) { cout << "Creat a indent" << endl; }
    ~Indent() { cout << "Delete a indent" << endl; }
    Indent(Indent& x) = delete;
    Indent(Indent&& x) = delete;
    Indent operator=(Indent& x) = delete;
    Indent operator=(Indent&& x) = delete;
    void printindent() const { cout << "订单号:" << account << " 餐品名称:" << food_name << endl; }
};

Restaurant:餐厅类

按照队列结构,设计队头head指针,队尾tail指针,num为订单数量。

push函数即入队操作,因为在构造函数里我默认队列为空,因此在入队第一个节点时,使队头队尾都指向它;当队列不为空后,从队尾入队,队尾指针自动向下一级移动。

Pop函数即出队操作,在确认队列不为空的条件下,从队头出队并打印输出订单信息。这里要注意,如果队列中只剩下一个节点时,务必要将head和tail变成nullptr,否则会出现空悬指针问题。

class Restaurant {
private:
	Indent* head;
	Indent* tail;
	int num;
public:
	Restaurant() :head(nullptr), tail(nullptr), num(0) { cout << "Creat a restaurant" << endl; }
	~Restaurant() { cout << "Delete a restaurant" << endl; }
	Restaurant(Restaurant& x) = delete;
	Restaurant(Restaurant&& x) = delete;
	Restaurant operator=(Restaurant& x) = delete;
	Restaurant operator=(Restaurant&& x) = delete;
	void printRestaurant() const {
		Indent* p = head;
		while (p != nullptr) {
			p->printindent();
			p = p->next;
		}
	}
	bool push(int account, string food_name) {
		Indent* t = new Indent(account, food_name);
		if (head == nullptr && tail == nullptr) {
			head = tail = t;
		}
		else {
			tail->next = t;
			tail = tail->next;
		}
		num++;
		return true;
	}
	bool Pop() {
		if (head == nullptr) {
			return false;
		}
		if (head != tail) {
			Indent* p = head;
			p->printindent();
			head = head->next;
			delete p;
			num--;
		}
		else {
			head->printindent();
			delete head;
			head = tail = nullptr;
			num = 0;
		}
		return true;
	}
	bool Delete_Restaurant() {
		while (num != 0) {
			Pop();
		}
		return true;
	}
};

main:测试代码

int main() {
	Restaurant 楞敲小青年的餐厅;
	楞敲小青年的餐厅.push(1, "香辣鸡腿堡");
	楞敲小青年的餐厅.push(2, "香辣鸡肉卷");
	楞敲小青年的餐厅.push(3, "香辣鸡翅");
	楞敲小青年的餐厅.printRestaurant();
	楞敲小青年的餐厅.Pop();
	楞敲小青年的餐厅.printRestaurant();
	楞敲小青年的餐厅.Delete_Restaurant();
	return 0;
}

运行结果

运行结果

 源代码

#include<iostream>
#include<string>
using namespace std;
class Indent {
public:
	int account;
	string food_name;
	Indent* next;
	Indent(int a, string fn, Indent* n = nullptr) :account(a), food_name(fn), next(n) { cout << "Creat a indent" << endl; }
	~Indent() { cout << "Delete a indent" << endl; }
	Indent(Indent& x) = delete;
	Indent(Indent&& x) = delete;
	Indent operator=(Indent& x) = delete;
	Indent operator=(Indent&& x) = delete;
	void printindent() const { cout << "订单号:" << account << " 餐品名称:" << food_name << endl; }
};
class Restaurant {
private:
	Indent* head;
	Indent* tail;
	int num;
public:
	Restaurant() :head(nullptr), tail(nullptr), num(0) { cout << "Creat a restaurant" << endl; }
	~Restaurant() { cout << "Delete a restaurant" << endl; }
	Restaurant(Restaurant& x) = delete;
	Restaurant(Restaurant&& x) = delete;
	Restaurant operator=(Restaurant& x) = delete;
	Restaurant operator=(Restaurant&& x) = delete;
	void printRestaurant() const {
		Indent* p = head;
		while (p != nullptr) {
			p->printindent();
			p = p->next;
		}
	}
	bool push(int account, string food_name) {
		Indent* t = new Indent(account, food_name);
		if (head == nullptr && tail == nullptr) {
			head = tail = t;
		}
		else {
			tail->next = t;
			tail = tail->next;
		}
		num++;
		return true;
	}
	bool Pop() {
		if (head == nullptr) {
			return false;
		}
		if (head != tail) {
			Indent* p = head;
			p->printindent();
			head = head->next;
			delete p;
			num--;
		}
		else {
			head->printindent();
			delete head;
			head = tail = nullptr;
			num = 0;
		}
		return true;
	}
	bool Delete_Restaurant() {
		while (num != 0) {
			Pop();
		}
		return true;
	}
};
int main() {
	Restaurant 楞敲小青年的餐厅;
	楞敲小青年的餐厅.push(1, "香辣鸡腿堡");
	楞敲小青年的餐厅.push(2, "香辣鸡肉卷");
	楞敲小青年的餐厅.push(3, "香辣鸡翅");
	楞敲小青年的餐厅.printRestaurant();
	楞敲小青年的餐厅.Pop();
	楞敲小青年的餐厅.printRestaurant();
	楞敲小青年的餐厅.Delete_Restaurant();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楞敲小青年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值