将项目附加到链接列表
Append Item to Linked List
所以我完全不知道如何将节点附加到链表中。基本上,我有一个Inventory对象(链表),由包含ItemStack对象的节点组成。此ItemStack对象包含项目的名称和该项目的数量。在Inventory.cpp文件中,我有一个名为addItems()的函数,它有一个ItemStack参数。
出于某种原因,当我试图将ItemStack节点附加到列表的末尾并输出结果时,唯一的输出是第一个和最后一个ItemStack。它跳过在中间的ItemStack节点。我不确定它们是否被覆盖或发生了什么。我知道这与我的Inventory.cpp文件或Inventory.h文件有关,因为我不允许修改任何其他文件。
该程序的输入是从两个文件中读取的。itemList-01.txt和inventoryList-01.txt。此外,库存中已占用插槽的数量没有更新。如果有人愿意对我所面临的问题有所了解,我将不胜感激。我已经做了好几天了,没有取得任何进展。如果下面的代码不够有用,我会附上一个链接到我的所有代码。(请记住,只能修改INVENTORY.PP和INVENTORY.H)。提前谢谢。
https://github.com/brussell757/CS330/tree/master/Assignment_1
启动INVENTORY.H文件
#ifndef INVENTORY_H_INCLUDED
#define INVENTORY_H_INCLUDED
#include <iostream>
#include "ItemStack.h"
/**
* An Inventory is composed of n slots. Each slot may store only
* one type of item--specified by *slots*.
* <p>
* Once all slots are filled, no additional Item types may be
* stored. Individual slots may contain any number of the same
* Item.
*/
class Inventory{
private:
/**
* Each Node represents one Inventory slot--i.e., space
*/
struct Node{
ItemStack data; ///< One ItemStack
Node *next; ///< Next ItemStack Node
/**
* Create an empty *Air* Node
*/
Node();
/**
* Create a Node that contains an ItemStack, *s*
*/
Node( ItemStack s );
};
Node *first; ///< First inventory slot
Node *last; ///< Last inventory slot
int slots; ///< Capacity
int occupied; ///< Number of occupied slots
/**
* Disassembles the list for Deconstructor
*/
void disassemble();
public:
/**
* Default to 10 slots
*/
Inventory();
/**
* Create an inventory with n slots
*
* @pre n > 0
*/
Inventory( int n );
/**
* Copy an already existing Inventory
*/
Inventory( const Inventory &src );
/**
* Destruct an Inventory
*/
~Inventory();
/**
* Add one or more items to the inventory list
*
* @return true if *stack* was added and false otherwise
*/
bool addItems( ItemStack stack );
/**
* Print a Summary of the Inventory and all Items contained within
*/
void display( std::ostream &outs ) const;
/**
*
*/
Inventory::Node* begin() const;
/**
*
*/
Inventory::Node* end() const;
/**
* Overloaded assignment operator for Inventory
*/
Inventory& operator=( const Inventory &rhs );
};
/**
* Print the Inventory through use of the display member function
*/
inline std::ostream& operator<<(std::ostream &outs, const Inventory &prt) {
prt.display( outs );
return outs;
}
#endif
INVENTORY.CPP文件的启动
/**
* Used to add items to the Inventory
*/
bool Inventory::addItems ( ItemStack stack ){
Node* new_node = nullptr;
// Sets new_node equal to a new node containing the current ItemStack
new_node = new Node(stack);
// Insert ItemStack into empty Inventory
if(this->first == nullptr) {
// Sets the first node in the Inventory to the new Node
this->first = new_node;
// Sets the last node in the Inventory to the new Node
this->last = new_node;
// Increase the number of occupied slots by 1
occupied++;
return true;
} else {
// Statement that executes if the maximum number of slots in the Inventory have not been filled
if(occupied <= slots) {
// Sets current node to the head
Node *curr = this->first;
// Sets trail node to nullptr
Node *trail = nullptr;
// Traverse the list
while(curr != nullptr) {
// Sets the (first->next) node to the new_node (new ItemStack)
curr->next = new_node;
// Sets the trail node to the current node (first)
trail = curr;
// Sets the current node to the node after new_node (nullptr)
curr = new_node->next;
return true;
}
// Increase the number of occupied slots by 1
occupied++;
} else {
return false;
}
}
}
在循环中,您应该遍历列表,直到到达末尾。到达列表末尾后,然后添加节点。
没有测试,但这是你很可能应该做的:
Node *curr = this->first;
Node *temp = curr;
while(curr != nullptr)
{
temp = curr;
curr = curr->next;
}
temp->next = new_node;
new_node->next = nullptr;
last = new_node;
这只是遍历列表,直到列表结束。临时节点跟踪最后一个节点。完成后,只需将最后一个有效节点指向新节点即可。
相关文章:
- 从链接列表c++中删除一个项目
- 读取文件的最后一行并输入到链接列表时出错
- 下面是我为检测链接列表中的循环而制作的代码
- 有人能帮我处理这个链接列表吗?C++
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 链接列表运算符重载没有打印出我想要的内容
- 链接列表在 cpp 中包含不同的对象类
- 创建一个棋盘格或"Interweave"两个链接列表。IE 更改两个链表的指针
- 如何在构建链接列表时调整头、尾指针
- Shared_ptr双链接列表内存泄漏
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 在链接列表中查找元素 - C++
- 删除链接列表中剩余的最后一个节点
- 只有我的"链接列表"中的第一个节点正在打印
- 避免在使用链接列表从 deque 中删除最后一个节点时出现内存泄漏
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 如何在C / C++中正确实现链接列表而不会使程序崩溃
- 将元素插入链接列表