【问题标题】:Append Item to Linked List将项目附加到链接列表
【发布时间】: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


【解决方案1】:

在您的循环中,您应该遍历列表直到到达末尾。一旦你到达列表的末尾,然后你添加节点。

未经过测试,但您很可能应该这样做:

    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;

这只是遍历列表直到它到达末尾。临时跟踪最后一个节点。然后,一旦完成,您只需将最后一个有效节点指向新节点。

【讨论】:

  • 非常感谢您的建议,我会尝试一下,看看效果如何。
  • @Brandon 然后你的链表类在你得到这个代码之前就被破坏了。我发布的代码中没有任何内容会导致无限循环。如果仅仅循环直到你到达最后是一个问题,那么你的代码有更大的问题需要你去寻找。
  • @Brandon 我看了你的其他代码。 Inventory 的复制构造函数已损坏。因此,如果您希望 addItems 工作,则无法满足您不能更改任何其他代码的要求。当您的测试包含vector&lt;Inventory&gt; 时,我立即注意到了这一点,唯一可行的方法是Inventory 是否可以安全地复制和分配,不幸的是,它不是。您的复制构造函数会创建假副本,这样做会导致未定义的行为,即使不是不良行为。
  • 您还有一个损坏的parseInventoryFile 函数。这个函数没有理由在任何地方使用new Inventory
  • 不允许操作该代码。只允许触摸 Inventory.h 和 Inventory.cpp。我终于让它工作了。我遇到这么多麻烦的主要原因是因为我在我的复制构造函数中设置了占用等于 src.occupied,而它应该设置为 0。如果你想看一下,我在 github 上更新的代码是工作代码。
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 2011-05-15
  • 2011-10-12
  • 2013-12-24
  • 1970-01-01
  • 2021-08-01
  • 2011-01-31
  • 2021-07-11
相关资源
最近更新 更多