【问题标题】:How to create a previous pointer in a linked list in C++?如何在 C++ 的链表中创建前一个指针?
【发布时间】:2012-04-18 20:58:58
【问题描述】:

我创建了一个 LinkedList 类,该类具有删除列表中第一个元素和删除列表中最后一个元素的功能。第一个很简单,删除元素后,我将它设置为指向下一个元素。效果很好。但是,当我删除最后一个元素时,我必须将它指向列表中的前一个元素,该元素成为该点的最后一个元素。我无法弄清楚如何做到这一点。请指教。代码如下:

    void LinkedList::pop_front()
{
    mFront->data = NULL;
    mFront = mFront->next;
}

如何获得一个函数来删除最后一个元素但将尾部重置为指向新的尾部?

void LinkedList::pop_back()
{
mBack->data = NULL;
...
}

class LinkedList
{
    public:

        // Default Constructor
        // Purpose: Initializes an empty list
        // Parameters: none
        // Returns: none
        LinkedList();

        // The push_front function
        // Purpose: add an item to the front of the list
        // Parameters: a int item for the front
        // Returns: none        
        void push_front(int data);

        // The push_back function
        // Purpose: insert an item into the back of the list
        // Parameters: int item to add the the back
        // Returns: none                
        void push_back(int data);

        // The pop_front function
        // Purpose: delete the item in the front of the list
        // Parameters: none
        // Returns: none
        void pop_front();

        // the pop_back function
        // Purpose: remove the item at the end of the list
        // Parameters: none
        // Returns: none        
        void pop_back();

        // The getFirst function
        // Purpose: print the first item in the list
        // Parameters: none
        // Returns: none
        void getFirst();

        // the GetLast function
        // Purpose: return the last item in the list
        // Parameters: none
        // Returns: none
        void getLast();

        void printList();

        // the clear function
        // Purpose: clear the list, free memory
        // Parameters: none
        // Returns: none
        void clear();

        // Destructor
        // Purpose: clear up memory
        // Parameters: none
        // Returns: none
        ~LinkedList();

    private:

            LinkedList *mFront; //  point to the front of our list
            LinkedList *mBack; // point to the back of our list
            LinkedList *next;  // the next node
            LinkedList *previous; // the previous node
            int data;  // our list data manipulator

【问题讨论】:

  • 如果你想要 O(1) 移除元素,你需要一个 doubly linked list(即所有节点上的 nextprev 指针)。
  • 如果这是家庭作业,添加homework标签是个好主意,这样人们可以尝试给出更多的解释和更少的代码。

标签: c++ linked-list


【解决方案1】:

单链表不提供 O(1) 删除最后一个元素。您必须从头开始遍历整个列表才能找到倒数第二个元素。

Node* i = mFront;
while ( i->next != mBack ) i = i->next;
mBack = i;

【讨论】:

  • 它说节点未定义。我是初学者,我需要在那里引用类的数据成员吗?或者,创建一个新指针?我编辑了上面的代码以显示我的类名和数据成员。
【解决方案2】:

如果列表不是双链接的,则必须从第一个元素开始才能找到最后一个:

void LinkedList::pop_back()
{
   mBack->data = NULL;
   current = mFront;
   do{
      current = current->next
   } while ( current->next );
   mBack = current;
}

非常重要 - 由于data 似乎是一个指针,您可能会遇到内存泄漏。仅设置 data = NULL 不会释放内存,您必须明确删除它:

delete mFront->data;

【讨论】:

  • 您的代码也给了我错误。我不确定当前引用的是什么,我更改了上面的代码来代表我的班级
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 2020-03-26
  • 2014-02-25
  • 2016-02-08
  • 2013-05-09
  • 2023-03-22
相关资源
最近更新 更多