【发布时间】:2015-04-16 11:38:04
【问题描述】:
所以我完全不知道如何将节点附加到我的链表中。基本上,我有一个 Inventory 对象(链表),由包含 ItemStack 对象的节点组成。这个 ItemStack 对象包含一个项目的名称和该项目的数量。在我的 Inventory.cpp 文件中,我有一个名为 addItems() 的函数,它有一个 ItemStack 参数。
由于某种原因,当我尝试将 ItemStack 节点附加到列表末尾并输出结果时,唯一的输出是第一个和最后一个 ItemStack。它跳过了中间的 ItemStack 节点。我不确定它们是否只是被覆盖或发生了什么。我知道这与我的 Inventory.cpp 文件或 Inventory.h 文件有关,因为我不允许修改任何其他文件。
这个程序的输入是从两个文件中读取的。 itemList-01.txt 和 inventoryList-01.txt。此外,库存中占用的插槽数量不会被更新。如果有人愿意对我遇到的问题有所了解,我将不胜感激。我已经做了好几天了,没有任何进展。如果以下代码不够有用,我将附上我所有代码的链接。(请记住,只有库存.CPP 和库存.H 可以修改)。提前致谢。
https://github.com/brussell757/CS330/tree/master/Assignment_1
库存开始.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
库存开始.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;
}
}
}
【问题讨论】:
-
请使用段落。问题中必须包含相关代码。
-
知道了,很抱歉
-
请发布您的
Inventory类定义。另外,为什么要添加到列表末尾的循环在循环中执行各种操作?不是应该先遍历到链表的末尾,到了之后再添加节点吗? -
我厌倦了,但由于某种原因,我在这样做时遇到了分段错误错误
标签: c++ loops linked-list append nodes