Akshat Karani
Akshat Karani

Reputation: 73

Doubly Linked List - cannot in delete the first Node

 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

Upvotes: 1

Views: 494

Answers (3)

Laeeq Khan Niazi
Laeeq Khan Niazi

Reputation: 648

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/

Upvotes: -3

leiyc
leiyc

Reputation: 963

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

Upvotes: 1

Guy haimovitz
Guy haimovitz

Reputation: 1625

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

Upvotes: 3

Related Questions