双链表-无法删除第一个节点

时间:2018-08-11 08:30:56

标签: c++ doubly-linked-list

 struct Node
{
int data;
Node *next;
Node *prev;
};

class DoublyLinkedList
{
ofstream cout3;
Node *head; 
public:
DoublyLinkedList()
{
    head = NULL;
    cout3.open("task3.out");
}

void insert(int num)
{
    Node *temp = new Node;
    //To insert if there are no elements
    if(head == NULL)
    {
        temp->prev = NULL;
        temp->data = num;
        temp->next = NULL;
        head = temp;
    }
    //To insert if there are elements
    else
    {
        temp->prev = NULL;
        temp->data = num;
        temp->next = head;
        head->prev = temp;
        head = temp;
    }       
    cout3<<"inserted "<<num<<endl;      
}

void dele(int num)
{
    Node *temp = head;
    int found_num = 0;
    while(temp != NULL)
    {
        if(temp->data == num)
        {
            found_num = 1;
            break;
        }   
        else
            temp = temp->next;
    }
    if(found_num == 0)
        cout3<<"cannot delete "<<num<<endl;
    //To delete first element
    else if (temp->prev == NULL)
    {
        head = temp->next;
        (temp->next)->prev == NULL;
        delete temp;            
        cout3<<"deleted "<<num<<endl;
    }
    //To delete last element
    else if (temp->next == NULL)
    {
        (temp->prev)->next = NULL;
        cout3<<"deleted "<<num<<endl;
        delete temp;
    }
    //To delete any other element
    else
    {
        (temp->prev)->next = temp->next;
        (temp->next)->prev = temp->prev;
        cout3<<"deleted "<<num<<endl;
        delete temp;
    }
}

void search(int num)
{
    Node *temp = head;
    int found_num = 0;
    while(temp != NULL)
    {
        if(temp->data == num)
        {
            found_num = 1;
            break;
        }   
        else
            temp = temp->next;
    }
    if(found_num == 0)
        cout3<<"not found "<<num<<endl;
    else
        cout3<<"found "<<num<<endl;
}

void display()
{
    Node *temp = head;
    while(temp != NULL)
    {
        cout3<<temp->data<<" ";
        temp = temp->next;
    }
    cout3<<endl;
}
};

我对双链表的实现。 我只在开头插入并删除该数字的第一个出现。 但是,如果我想删除第一个元素,那么它将打印“已删除的数字”,但是当我显示数字时,该数字仍然存在。 问题似乎出在我的删除功能中,但我找不到它

3 个答案:

答案 0 :(得分:3)

请参阅以下行:(temp-> next)-> prev == NULL; 您写了==而不是=,这似乎是问题所在。 您没有显示如何打印该值,但是我猜想您将向后移动直到开始之前为null值。

答案 1 :(得分:1)

只需花费代码对其进行测试,它就会发出警告。修复它,程序将按预期运行。

$ g++ test.cpp 
test.cpp:66:30: warning: equality comparison result unused
      [-Wunused-comparison]
          (temp->next)->prev == NULL;
          ~~~~~~~~~~~~~~~~~~~^~~~~~~
test.cpp:66:30: note: use '=' to turn this equality comparison into an
      assignment
          (temp->next)->prev == NULL;
                             ^~
                             =
1 warning generated.

test.cpp

#include <iostream>
#include <fstream>

struct Node
{
  int data;
  Node *next;
  Node *prev;
};

class DoublyLinkedList
{
  std::ofstream cout3;
  Node *head; 

public:
  DoublyLinkedList()
  {
      head = NULL;
      cout3.open("task3.out");
  }

  void insert(int num)
  {
      Node *temp = new Node;
      //To insert if there are no elements
      if(head == NULL)
      {
          temp->prev = NULL;
          temp->data = num;
          temp->next = NULL;
          head = temp;
      }
      //To insert if there are elements
      else
      {
          temp->prev = NULL;
          temp->data = num;
          temp->next = head;
          head->prev = temp;
          head = temp;
      }       
      cout3<<"inserted "<<num<<std::endl;      
  }

  void dele(int num)
  {
      Node *temp = head;
      int found_num = 0;
      while(temp != NULL)
      {
          if(temp->data == num)
          {
              found_num = 1;
              break;
          }   
          else
              temp = temp->next;
      }
      if(found_num == 0)
          cout3<<"cannot delete "<<num<<std::endl;
      //To delete first element
      else if (temp->prev == NULL)
      {
          head = temp->next;
          (temp->next)->prev == NULL;
          delete temp;            
          cout3<<"deleted "<<num<<std::endl;
      }
      //To delete last element
      else if (temp->next == NULL)
      {
          (temp->prev)->next = NULL;
          cout3<<"deleted "<<num<<std::endl;
          delete temp;
      }
      //To delete any other element
      else
      {
          (temp->prev)->next = temp->next;
          (temp->next)->prev = temp->prev;
          cout3<<"deleted "<<num<<std::endl;
          delete temp;
      }
  }

  void search(int num)
  {
      Node *temp = head;
      int found_num = 0;
      while(temp != NULL)
      {
          if(temp->data == num)
          {
              found_num = 1;
              break;
          }   
          else
              temp = temp->next;
      }
      if(found_num == 0)
          cout3<<"not found "<<num<<std::endl;
      else
          cout3<<"found "<<num<<std::endl;
  }

  void display()
  {
      Node *temp = head;
      while(temp != NULL)
      {
          cout3<<temp->data<<" ";
          temp = temp->next;
      }
      cout3<<std::endl;
  }
};

int main()
{
   DoublyLinkedList list;
   list.insert(3);
   list.insert(4);
   list.insert(5);
   list.display();

   list.dele(3);
   list.display();  
}

答案 2 :(得分:-3)

在下面的链接中,有释放内存的指南。 Delete方法通常无法使用free方法。

https://www.geeksforgeeks.org/write-a-function-to-delete-a-linked-list/