簡體   English   中英

C ++鏈表混淆

[英]C++ Linked List confusion

我一直在試圖弄清楚如何實現基於NODE的鏈接列表,該列表可以保存任何內容(通過模板)。

我已經准備好Node類並可以正常工作(已通過Stack類進行了測試)。

我很好奇,在做一個名為insertAtIndex的函數時,我可能會出錯,在這里我要存儲數據並在應該將數據存儲在索引處。

我的節點類

template <class T>
class Node
{
    public:
    T *data; //the object information
    Node<T> *next; //pointer to the next node element

    Node()
    {
        next = 0;
        data = 0;
    }
};

到目前為止,這是我的鏈接列表課程

template <class T>
class LinkedList
{
private:
    Node<T> *head;
    int count;
public:
    LinkedList()
    {
        head = 0;
        count = 0;
    }
    int getCount()
    {
        return count;
    }

    void insertAtIndex(T* dat, int index)
    {   
        Node<T> * temp = new Node<T>(dat);

        if(index == 0)
        {

            temp = head;
            temp->data  = dat;
            temp->next = temp->next;
            temp->next = temp;
            delete temp;
            count++;
        }

        else if(index <= count)
        {
            Node<T> *cursor = new Node<T>();

            for(int i = 0; i < index - 1; i++)
            {
                cursor = cursor->next;
            }

            Node<T> * temp = new Node<T>();
            temp->data  = dat;
            temp->next = cursor->next;
            cursor->next = temp;
            count++;
        }

    }

    void Print()
    {

        // Temp pointer
        Node<T> *tmp = head;

        // No nodes
        if ( tmp == NULL ) {
        cout << "EMPTY" << endl;
        return;
        }

        // One node in the list
        if ( tmp->next == NULL ) {
        cout << *tmp->data;
        cout << " --> ";
        cout << "NULL" << endl;
        }
        else 
        {
        // Parse and print the list
        do
        {
            cout << *tmp->data;
            cout << " --> ";
            tmp = tmp->next;
        }
        while ( tmp != NULL );

        cout << "NULL" << endl;
        }
    }
};

您應該將Node(T *)構造函數添加到Node類中:

template <class T>
class Node
{
    public:
    T *data; //the object information
    Node<T> *next; //pointer to the next node element

    Node()
    {
        next = 0;
        data = 0;
    }

    Node(T* newData) { // <<---------- Here it is
        next = 0;
        data = newData;
    }
};

您需要重新考慮如何將節點添加到列表中。 這是您的代碼,其中包含有關其實際操作的注釋。

   Node<T> * temp = new Node<T>(dat);  // Create new node, good

    if(index == 0)
    {

        temp = head;                  // Throw away pointer to new node, bad. temp is now head.
        temp->data  = dat;            // Overwrite head's data, bad
        temp->next = temp->next;      // Set a variable to itself, does nothing.
        temp->next = temp;            // Change head's next to point to itself, bad
        delete temp;                  // delete the head node, bad
        count++;
    }

您實際要做的是:

  • 使用正確的數據創建一個新節點(您已經執行了此操作)
  • 如果將其放在列表的開頭:
    • 將新節點的下一個指針指向head
    • 更改頭以指向新節點

僅此而已-請勿刪除任何內容。

完成工作后,繼續執行將節點添加到中間或列表的其他更復雜的部分(其他部分)。

調試器(或至少是輸出語句)對於了解代碼出問題的地方至關重要。 您可以在每個點查看變量的狀態。

您可能要考慮只將下一個指針作為成員的基節點類,然后考慮一個添加模板數據成員的繼承類。 通用列表函數可用於基類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM