简体   繁体   English

C ++链表混淆

[英]C++ Linked List confusion

I've been stuck trying to figure out how I can implement a NODE based link list that can hold anything (via templates). 我一直在试图弄清楚如何实现基于NODE的链接列表,该列表可以保存任何内容(通过模板)。

I've got the Node class ready and working(tested with my Stack class). 我已经准备好Node类并可以正常工作(已通过Stack类进行了测试)。

I was curious where I could be going wrong when making the function called insertAtIndex where I take in the data to store and the index at where it should be stored. 我很好奇,在做一个名为insertAtIndex的函数时,我可能会出错,在这里我要存储数据并在应该将数据存储在索引处。

My Node class 我的节点类

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;
    }
};

Here's my Linked List class so far 到目前为止,这是我的链接列表课程

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;
        }
    }
};

You should add Node(T*) constructor to your Node class: 您应该将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;
    }
};

You need to rethink how you are adding nodes to the list. 您需要重新考虑如何将节点添加到列表中。 Here is your code with comments on what it is actually doing. 这是您的代码,其中包含有关其实际操作的注释。

   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++;
    }

What you actually want to do is: 您实际要做的是:

  • Create a new node with the right data (you already do this) 使用正确的数据创建一个新节点(您已经执行了此操作)
  • If you are putting it at the start of the list: 如果将其放在列表的开头:
    • Point the new node's next pointer to head 将新节点的下一个指针指向head
    • Change head to point to the new node 更改头以指向新节点

That's all - don't delete anything. 仅此而已-请勿删除任何内容。

Once you have that working, move on to the more complicated part of adding a node to the middle or the list (your else part). 完成工作后,继续执行将节点添加到中间或列表的其他更复杂的部分(其他部分)。

A debugger (or at least output statements) is essential for learning where your code is going wrong. 调试器(或至少是输出语句)对于了解代码出问题的地方至关重要。 You can look at the state of your variables at each point. 您可以在每个点查看变量的状态。

You might want to consider a base node class that only has a next pointer as a member, then an inherited class that adds a template data member. 您可能要考虑只将下一个指针作为成员的基节点类,然后考虑一个添加模板数据成员的继承类。 Common list functions could be used for the base class. 通用列表函数可用于基类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM