将项目附加到链接列表

Append Item to Linked List

本文关键字:链接 列表 项目      更新时间:2023-10-16

所以我完全不知道如何将节点附加到链表中。基本上,我有一个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;

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