如何删除C++中添加到链表中的最后一个元素



我第一次使用指针。我有一个程序,它可以在链表中插入数字,打印列表,并从列表中删除特定的数字。它有效,除非我试图删除最后插入的号码。

节点.h

#ifndef Node_h
#define Node_h
#include <iostream>
using namespace std;
class Node
{
public:
    int data;
    Node *next;
public:
Node();
};
#endif

Node.cpp

#include "Node.h"
Node::Node()
{
}

LinkedList.h

#ifndef LinkedList_h
#define LinkedList_h
#include "Node.h"
class LinkedList
{
    private:
    Node *pL;
public:
    LinkedList();
    void insert(int nr1);
    void deleteNr(int nr1);
    void printL();
};
#endif

链接列表.cpp//该程序正在创建数字的"链接列表"

#include "LinkedList.h"
LinkedList::LinkedList()
{
    pL = NULL;
}
void LinkedList::insert(int nr1)
{
    Node *p = new Node;
    p->data = nr1;
    p->next = pL;
    pL = p;
}
void LinkedList::deleteNr(int nr1)
{
    Node *p = pL;
    Node *p2 = pL;
    while (p != NULL & p->data != nr1)
    {
        p2 = p;
        p = p->next;
    }
    if (p != NULL)
    {
        p2->next = p->next;
        delete p;
    }
}
void LinkedList::printL()
{
    Node *p = pL;
    while (p != NULL)
    {
        cout << p->data << "-> ";
        p = p->next;
    }
}

main.cpp

#include "LinkedList.h"
int menu();
//////// main /////////
int main()
{
    int choice1, nr1;
    LinkedList lk1;
    choice1 = menu();
    while (choice1 <= 3)
    {
        if (choice1 == 1)
        {
            cout << "Enter number." << endl;
            cin >> nr1;
            lk1.insert(nr1);
        }
        else if (choice1 == 2)
        {
            cout << "Enter number." << endl;
            cin >> nr1;
            lk1.deleteNr(nr1);
        }
        else if (choice1 == 3)
        {
            lk1.printL();
            cout << endl << endl;
        }
        else if (choice1 == 4)
        {
            cout << "Exit the program." << endl;
            system("pause");
            exit(1);
        }
        choice1 = menu();
    } // end while loop
}
int menu()
{
    int choice1;
    cout << "1. Insert a number into the linked-list." << endl;
    cout << "2. Delete a number from the linked-list." << endl;
    cout << "3. Print the linked-list." << endl;
    cout << "4. Exit the program." << endl;
    cout << "Enter choice." << endl;
    cin >> choice1;
    return choice1;
}

您的问题是,通常情况下,p2是列表中p后面的一个节点,但如果要删除第一个节点,那么delete函数中的第一个while循环有0次迭代,p2和p是相同的。头已删除,但pL未更新。它只是指向未分配的内存。这可能会使节点看起来没有被删除,或者可能会导致分段故障和崩溃。不管怎样,这都是错误的行为。您需要确保检查要删除的节点是第一个节点的情况,并更新pL.

试试这个

void LinkedList::deleteNr(int nr1)
{
    Node *p = pL;
    Node *p2 = pL;
    if(p != NULL && nr1 == p->data)
    {
        pL = p->next;
        delete p;
        return;
    }
    while (p != NULL && p->data != nr1)
    {
        p2 = p;
        p = p->next;
    }
    if (p != NULL)
    {
        p2->next = p->next;
        delete p;
    }
}

如果您想删除链表中nr1的所有实例,则需要添加另一个循环:

void LinkedList::deleteNr(int nr1)
{
    Node *p = pL;
    while(p != NULL && nr1 == p->data)
    {
        pL = p->next;
        delete p;
        p = pL;
    }
    Node *p2 = pL;
    while (p != NULL)
    {
        p2 = p;
        p = p->next;
        if(nr1 == p->data)
        {
            p2->next = p->next;
            delete p;
        }
    }
}

您需要添加代码来处理给定输入对应于列表中第一项的情况。

void LinkedList::deleteNr(int nr1)
{
   Node *p = pL;
   if ( p != NULL && p->data == nr1 )
   {
      pL = p->next;
      delete p;
      return;
   }
   Node *p2 = pL;
   while (p != NULL && p->data != nr1)
   {
      p2 = p;
      p = p->next;
   }
   if (p != NULL)
   {
      p2->next = p->next;
      delete p;
   }
}
void LinkedList::deleteLast()
{
Node *p = pL;
if(p == NULL)
   return;
else if(p->next == NULL) {
   p = NULL;
 }
 else {
  while (p->next->next != NULL)
  {
    p = p->next;
  }
  p->next = NULL; 
 }
}

相关内容

  • 没有找到相关文章

最新更新