将项目附加到链接列表

时间:2015-02-16 01:28:11

标签: c++ loops linked-list append nodes

所以我完全和完全混淆了如何将节点附加到我的链表。基本上,我有一个Inventory对象(链表),由包含ItemStack对象的节点组成。此ItemStack对象包含项目的名称和该项目的数量。在我的Inventory.cpp文件中,我有一个名为addItems()的函数,它有一个ItemStack参数。

出于某种原因,当我尝试将ItemStack节点附加到列表的末尾并输出结果时,唯一的输出是第一个和最后一个ItemStack。它会跳过中间的ItemStack节点。我不确定他们是否只是被覆盖或正在发生什么。我知道它与我的Inventory.cpp文件或我的Inventory.h文件有关,因为我不允许修改任何其他文件。

从两个文件中读取该程序的输入。 itemList-01.txt和inventoryList-01.txt。此外,库存中的占用插槽数量未被更新。如果有人愿意解决我所遇到的问题,我会非常感激。我已经好几天没有进展了。如果下面的代码没有足够的帮助,我会链接到我的所有代码。(请记住,只有INVENTORY.CPP和INVENTORY.H可以修改)。提前谢谢。

https://github.com/brussell757/CS330/tree/master/Assignment_1

START OF INVENTORY.H FILE

   #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

START OF INVENTORY.CPP FILE

    /**
    * 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;

        }

    }

} 

1 个答案:

答案 0 :(得分:0)

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

未经测试,但这是您应该做的事情:

    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;

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