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

时间:2014-10-28 17:59:32

标签: c++ linked-list

我第一次使用指针。我有一个程序将数字插入链表,打印列表,并从列表中删除特定的数字。除非我尝试删除最后插入的数字,否则它会起作用。

Node.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

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

3 个答案:

答案 0 :(得分:1)

你的问题是通常,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;
        }
    }
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

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