title: Singly Linked List Infinite Loop
body: <p>It's been a week since i started learning about linked list and i only managed to learn about singly linked list. So today i implemented the linked list which i learned in c++ and while i tried to run it the code goes into an infinite loop of some random numbers. I tried debugging the code but i coudn't find whats so ever is wrong with the code. The code is below. Help is appreciated.Thanks</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  struct node{
  int data;
  node * next;
  };
  
  class singly{
  private:
  node * head,*tail;
  public:
  singly(){
      head=NULL;
      tail=NULL;
  }
  
  void createNode(int value){
      node * temp = new node;
      temp-&gt;data=value;
      temp-&gt;next=NULL;
      if(head==NULL){
          head=temp;
          tail=temp;
          temp=NULL;
      }
  
      else{
          tail-&gt;next=temp;
          tail=temp;
      }
  }
  
  void display(){
      node * temp = new node;
      head=temp;
  
      while(temp!=NULL){
          cout &lt;&lt; temp-&gt;data &lt;&lt; "\t" &lt;&lt; endl;
          temp-&gt;next=temp;
      }
  }
  
  void insert_end(int value){
      node*newnode = new node;
      node*temp = new node;
      newnode-&gt;data=value;
      newnode-&gt;next=NULL;
      temp=head;
  
      while(temp-&gt;next!=NULL){
          temp = temp-&gt;next;
      }
      temp-&gt;next=newnode;
  }
  
  void delete_node(){
      node*current = new node;
      node*previous = new node;
      current = head;
      while(current-&gt;next!=NULL){
          previous=current;
          current=current-&gt;next;
      }
  
      tail=previous;
      previous-&gt;next=NULL;
      delete current;
  }
  };
  
  int main(){
  
  singly lists;
  lists.createNode(32);
  lists.createNode(654);
  lists.createNode(34);
  lists.createNode(234);
  
  cout&lt;&lt;"\n--------------------------------------------------\n";
  cout&lt;&lt;"---------------Displaying All nodes---------------";
  cout&lt;&lt;"\n--------------------------------------------------\n";
  lists.display();
  cout&lt;&lt;"\n--------------------------------------------------\n";
  cout&lt;&lt;"-----------------Inserting At End-----------------";
  cout&lt;&lt;"\n--------------------------------------------------\n";
  lists.createNode(55);
  lists.display();
  cout&lt;&lt;"\n--------------------------------------------------\n";
  cout&lt;&lt;"-----------------Deleing At End-------------------";
  cout&lt;&lt;"\n--------------------------------------------------\n";
  lists.delete_node();
  lists.display();
  }
  </code></pre>