title: Can anyone tell me why my program is going in infinite Loop?
body: <p>Why my code is giving an Infinite loop and i have checked my code many times and I don't know where the mistake is but my output is 34 in an infinite loop and it is a code for doubly linked list!</p>
  
  <p>So please tell me where the mistake is as well as why my loop is showing 34 instead of 77 in my code and is there any problem in my print function?   </p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  struct Node
  {
      int data;
      Node *next;
      Node *prev;
  };
  
  class Linked
  {
  public:
      Node *head;
      Node *tail;
      Linked()
      {
          head=NULL;
          tail=NULL;
      }
  
      void add(int data,int position)
      {
          Node *n=new Node;
          Node *n1=new Node;
          n-&gt;data=data;
          n-&gt;next=NULL;
          n-&gt;prev=NULL;
          if(head==NULL)
          {
              head=n;
              tail=n;
              cout&lt;&lt;"Linked list formed"&lt;&lt;endl;
          }
          if(position==0)
          {
              head-&gt;prev=n;
              n-&gt;next=head;   
              head=n;
              cout&lt;&lt;"Linked list created"&lt;&lt;endl;
  
          }
          else
          {
  
              n1=head;
              int posi=position;
              while(posi&gt;1)
              {
                  n1=n1-&gt;next;
                  posi--;
              }
              n-&gt;next=n1-&gt;next;
              n-&gt;next-&gt;prev=n;
              n1-&gt;next=n;
              n-&gt;prev=n1;
          }
      }
  
  
      void print()
      {
          Node *n=new Node;
          n=head;
          while(n!=NULL)
          {
              cout&lt;&lt;n-&gt;data&lt;&lt;endl;
              n=n-&gt;next;
          }
      }
  
  };
  int main()
  {
      Linked l;
  
      l.add(34,0);
      l.add(77,0);
      // l.add(44,1);
      // l.add(90,2);
      l.print();
      return 0;
  }
  </code></pre>