我的removeBack函数出了什么问题

What is wrong with my removeBack function?

本文关键字:什么 问题 removeBack 函数 我的      更新时间:2023-10-16

我制作了一个链表,我的其他函数也能工作,但我的removeBack不能。我遍历节点,检查它们是否为NULL,如果节点为NULL,我将链接器复制到指向倒数第二个的上一个指针,并删除包含NULL值的节点。我确信这是有效的,直到两个本应删除的数字没有被删除。这是我的密码。有人能带我到这儿来吗?

#include <string>
#include <iostream>
using namespace std;
class node
{
public:
    int data;
    node * next;
};
class linkedList
{
private:
    node * head;
public:
    linkedList()
    {
        head = NULL;
    }
void addFront(int x)
{
    node * babynode = new node;
    //(*babynode).data = x;
    babynode->data = x;
    babynode->next = head;
    head = babynode;
}
void display()
{
    node * finger = head;
    while( finger != NULL )
    {
        cout << finger->data << endl;
        finger = finger->next;
    }
}
//remove and return the first item in the list
void removeFront()
{
    if(head != NULL)
    {
    node * front = head;
    head = head->next;
    delete front;
    }
    else
    {
        head = NULL;
    }
}
void addBack(int x)
{   
    node * tail = head;
    //only if head is empty
    if(head == NULL)
    {
        addFront(x);
    }
    //if it isnt empty
    else{
        while(tail->next != NULL)
        {
            tail = tail->next;
        }           
            node * tail2 = new node;
            tail2 -> data = x;
            tail2 -> next = tail -> next;
            tail -> next = tail2;
        }
    }
void removeBack()
{   
    node * one = head;
    node * two = one -> next;
while(two != NULL)
{
    if(two == NULL)
    {
        delete one;
        head -> next = two;
    }
    one = one -> next;
    two = one -> next;
    }
}
};

Main:

#include <iostream>
using namespace std;
#include "myLinkedList.h"
int main()
{
    linkedList mylist;
    //Step 1:
    //Implement insertion to the front or the back of the list
    mylist.addBack(2);
    mylist.addBack(3);
    mylist.addFront(5);
    mylist.addFront(7);
    mylist.addBack(11);
    mylist.addBack(13);
    mylist.addBack(17);
    mylist.addFront(19);
    mylist.addFront(23);
    mylist.display(); //23 19 7 5 2 3 11 13 17
    //Step 2: 
    //Implement removal of the first or last item
    mylist.removeFront();
    mylist.removeFront();
    mylist.removeBack();
    mylist.removeBack();
    mylist.addFront(29);
    mylist.addBack(31);
    mylist.addBack(37);
    mylist.addBack(41);
    mylist.display(); //29 7 5 2 3 11 31 37 41
    ////Step 3:
    ////Implement a tricimation routine
    //mylist.tricmate(); //delete every 3rd item
    //mylist.display(); //29 7 2 3 31 37
    ////Step 4:
    ////Implement a find and remove method
    //mylist.remove(3);
    //mylist.remove(29);
    //mylist.remove(37);
    //mylist.addFront(43);
    //mylist.addBack(47);
    //mylist.display(); //43 7 2 31 47
    ////Step 5: sort!
    //mylist.sort();
    //mylist.display(); //2 7 31 43 47
    return 0;
}

您问:

我的removeBack函数出了什么问题?

我看到的是:

void removeBack()
{   
   node * one = head;
   node * two = one -> next; // When the list is empty, this
                             // will result in undefined behavior.
   while(two != NULL)
   {
      if(two == NULL) // This is never true.
                      // You've already checked in the while
                      // statement that two != NULL.
                      // Hence, you never end up deleting the item
                      // at the back of the list.
      {
         delete one;
         head -> next = two;
      }
      one = one -> next;
      two = one -> next;
   }
}

这应该有效:

void removeBack()
{   
   // First, check whether the list is empty
   if ( head == NULL )
   {
      return;
   }
   // Next, check whether there is only one item
   if ( head->next == NULL )
   {
      delete head;
      head = NULL;
      return;
   }
   // Now for the rest...
   // iterate to get to the end.
   node * iter = head;
   while ( iter->next->next != NULL )
   {
      iter = iter -> next;
   }
   delete iter->next;
   iter->next = NULL;
}