如何在C++中实现链表结构中的堆栈数据类型



我们要制作一个视频商店程序,但我一直被困在"租用视频";以及";返回视频";因为我在处理过程中必须使用堆栈。我的主要想法基本上是,每当客户租用时,租用的视频ID都会被推送到客户节点堆栈数据类型中。返回视频也是如此,它将从客户节点堆栈数据类型中弹出((返回的视频ID。(我不确定我是否应该使用链表,或者这是否是最好的方法。(

struct customerNode
{
int customer_ID;
string name, address;
stack <string> rentedVid;
stack <string> tempRent;
struct customerNode* next;
};
struct customerNode* first = NULL;
struct customerNode* last = NULL;
void insertCustomer(int custID, string custN, string custA)
{
struct customerNode* temp;
temp = new customerNode;
temp->customer_ID = custID;
temp->name = custN;
temp->address = custA;
// stack rentedVid ??
// stack tempRent ??
if (first == NULL)
{
first = temp;
last = temp;
}
else
{
last->next = temp;
last = temp;
}
}

(我还有一个视频ID、标题、副本等的链接列表(事实上,我对自己的项目没有信心。要求:当您从文本文件中检索到客户时,您必须将其存储在队列中(我现在使用了链表,但稍后会更改(。在处理过程中,它们还必须存储在队列中。当用户选择"退出程序"时,将保存回文本文件。租用的视频将存储在堆栈中,并在用户选择"退出程序"时保存在CUSTOMER-RENT文本文件中。

如果是业务应用程序,堆栈可能不是最佳选择。如果客户想租用多个视频而不按顺序归还,该怎么办?

cpp的vector是一个更好的选项,可以存储所有用户租用的视频并搜索/删除它们。链表也是一种很好的数据结构。链表的主要优点是,如果您在开头和结尾删除/插入元素,它是合适的,而不是随机访问。

最新更新