title: Deleting node from linked list
body: <p>The delete from last functionality does not work properly. It shows that the node has been deleted but when I display it, it gets into an infinite loop and displays junk. Couldn't figure what's wrong!</p>
  
  <p>Here's the code:</p>
  
  <pre><code>using namespace std;
  class List
  {
      struct NODE
      {
          int item;
          NODE *next;
      };
      NODE *Head,*Tail;
      public:
      List()
      {
          Head=NULL;
          Tail=NULL;
      }
      ~List()
      {
          while(Head-&gt;next!=NULL)
          {
              Delete_At_Head();
          }
          Delete_At_Head();
      }
      void Add_At_First(int);
      void Add_At_Last(int);
      void Delete_At_Head();
      void Delete_At_Tail();
      int Is_Empty();
      void display();
  };
  void List::Add_At_First(int data)
  {
      NODE *temp;
      temp=new NODE;
      if(Head==NULL)
      {
          temp-&gt;item=data;
          temp-&gt;next=NULL;
          Head=temp;
          Tail=Head;
      }
      else
      {
          temp-&gt;item=data;
          temp-&gt;next=Head;
          Head=temp;
      }
      cout&lt;&lt;"Node added at first!\n";
  }
  void List::Add_At_Last(int data)
  {
      NODE *temp;
      temp=new NODE;
      temp-&gt;item=data;
      temp-&gt;next=NULL;
      if(Head==NULL)
      {
          Head=temp;
          Tail=temp;
      }
      else
      {
          Tail-&gt;next=temp;
          Tail=temp;
      }
      cout&lt;&lt;"Node added at last!\n";
  }
  void List::Delete_At_Head()
  {
      NODE *temp;
      temp=new NODE;
      temp-&gt;item=Head-&gt;item;
      temp-&gt;next=Head-&gt;next;
      delete Head;
      Head=temp-&gt;next;
      delete temp;
      cout&lt;&lt;"Node deleted from head!\n";
  }
  void List::Delete_At_Tail()//Problematic part
  {
      NODE *temp,*prev;
      temp=new NODE;
      prev=new NODE;
      temp=Head;
      while(temp-&gt;next!=NULL)
      {
          prev=temp;
          temp=temp-&gt;next;
      }
      prev-&gt;next=NULL;
      delete temp;
      delete Tail;
      Tail=prev;
      delete prev;
      cout&lt;&lt;"Node deleted from tail!\n";
  }
  int List::Is_Empty()
  {
      if(Head==NULL)
          return 1;
      else
      return 0;
  }
  void List::display()//does not display after delete from tail
  {
      NODE *temp;
      temp=new NODE;
      temp-&gt;item=Head-&gt;item;
      temp-&gt;next=Head-&gt;next;
      do
      {
          cout&lt;&lt;temp-&gt;item&lt;&lt;"--&gt;";
          temp=temp-&gt;next;
      }while(temp-&gt;next!=NULL);
      cout&lt;&lt;temp-&gt;item;
  }
  int main()
  {
      List obj;
      int ch,data;
      do
      {
          cout&lt;&lt;"\n1.Display\n2.Add at first\n3.Add at last\n4.Delete at 
  head\n5.Delete at tail\n6.Exit\nEnter your choice: ";
          cin&gt;&gt;ch;
          switch(ch)
          {
              case 1:
              {
                  if(obj.Is_Empty())
                      cout&lt;&lt;"List is Empty!\n";
                  else
                      obj.display();
                  break;
              }
              case 2:
              {
                  cout&lt;&lt;"Enter data: ";
                  cin&gt;&gt;data;
                  obj.Add_At_First(data);
                  break;
              }
              case 3:
              {
                  cout&lt;&lt;"Enter data: ";
                  cin&gt;&gt;data;
                  obj.Add_At_Last(data);
                  break;
              }
              case 4:
              {
                  if(obj.Is_Empty())
                      cout&lt;&lt;"List is Empty!\n";
                  else
                      obj.Delete_At_Head();
                  break;
              }
              case 5:
              {
                  if(obj.Is_Empty())
                      cout&lt;&lt;"List is Empty!\n";
                  else
                      obj.Delete_At_Tail();
                  break;
              }
              case 6:
              {
                  break;
              }
          }
      }while(ch!=6);
      return 0;
  }
  </code></pre>