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

My implementation of Doubly Linked List. I only insert at the beginning and delete the first occurrence of the number. However if I want to delete the first element then it prints "deleted number" but when i display the number is still there. Problem seems to be in my delete function but I cannot find what it is

1

3 Answers 3

3

See this line: (temp->next)->prev == NULL; You wrote == instead of = , this seems to be the problem. You dont show how you print the value but im guessing you move backward untill null value before you start..

1

Just expend the code to test it, it will give out the warning. Fix it, then the program will function as expected.

$ 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
  • Thanks! But it does not generate a warning when I compile. My compiler version is gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3) Aug 11, 2018 at 13:07
  • @AkshatKarani, maybe my compiler(Apple LLVM version 9.1.0 (clang-902.0.39.2)) gives out the warning by default. For gcc/g++ under linux , you need to open the warning option(-Wall) explicitly. See the trials in coliru.stacked-crooked.com/a/2f595b25c3b2c335
    – leiyc
    Aug 11, 2018 at 15:43
-3

In below link there is a guide to free the memory. Delete function not work mostly you can free method.

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

7
  • Delete is basicly a wrapper for free(), and it's working as it should.. Aug 11, 2018 at 8:52
  • @Dr.Haimovitz deleteis not a wrapper for free. The two must not be used interchangeably. Aug 11, 2018 at 12:02
  • @Max Vollmer Delete is a wrapper for free and free() can be used instead, the difference is that free don't initialize the class d'tor. Aug 11, 2018 at 12:10
  • 1
    @Dr.Haimovitz Please don't spread this half-truth. While it may be that there are implementations of the language that implement delete as a wrapper of free, that is not defined by the language standard and thus should not be relied on in any way. In fact using delete for memory allocated with malloc or free on memory allocated with new is UB. Aug 11, 2018 at 12:47
  • 2
    @Dr.Haimovitz • mixing delete and free() on my system crashes the application. Because they are in separately managed heaps.
    – Eljay
    Aug 11, 2018 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.