title: adding node at ith position
body: <p>I want to create a link list with a size of 5 and add some nodes at ith position.</p>
  
  <p>I will add the nodes at random position say (0, 5 and 2) of the link list.</p>
  
  <p>this is what it looks like adding the node at position 0.</p>
  
  <pre><code>      0
   +---------+
   |    1    |
   +---------+ --&gt; NULL
   |  next   | 
   +---------+ 
  </code></pre>
  
  <p>this is what it looks like adding the node at position 5.</p>
  
  <pre><code>      0             1             2             3             4
   +---------+   +---------+   +---------+   +---------+   +---------+
   |    1    |   |  Empty  |   |  Empty  |   |  Empty  |   |    2    |
   +---------+--------------------------------------------&gt;+---------+--&gt;NULL
   |  next   |   |  node   |   |  node   |   |  node   |   |  next   |
   +---------+   +---------+   +---------+   +---------+   +---------+
  </code></pre>
  
  <p>so node 1,2,3 is empty and 0 is linked to 4.</p>
  
  <p>this is what it looks like adding the node at position 1.</p>
  
  <pre><code>      0             1             2             3             4 
   +---------+   +---------+   +---------+   +---------+   +---------+
   |    1    |   |    2    |   |  Empty  |   |  Empty  |   |    2    |
   +---------+--&gt;+---------+------------------------------&gt;+---------+--&gt;NULL
   |  next   |   |  next   |   |  node   |   |  node   |   |  next   |
   +---------+   +---------+   +---------+   +---------+   +---------+
  </code></pre>
  
  <p>so node 2,3 is empty and 0 is linked to 1 and 1 is linked to 4.</p>
  
  <p>I tried to implement it but it's not printing anything. Please advise. Thanks.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct node
  {
      int x;
      node *next;
  };
  
  node * head = NULL;
  node * newNode;
  node * temp;
  
  void addNode(int pos, int size)
  {
      /*if head is null, initialize a new node
        set data = 1 for head;
      */
      if(head == NULL &amp;&amp; pos == 0)
      {
          newNode = new node;
          head = newNode;
          head-&gt;x = 1;
          temp = head;
          temp-&gt;next=NULL;
      }
      else
      {
          /*
           Adding a node at ith position.
           1. check if the the position is less than the size of the link list.
           2. set the temp position to be 0(head)
           3. use the temp pointer and go to the ith postion.
           4. create new node at ith position.
           5. set data = 2 for the node at ith position.
       */
          if (pos &lt; size)
          {
              for(int i=0; i &lt; size; i++)
              {
                  temp = head;
                  temp = temp-&gt;next;
  
                  if (pos == i)
                  {
                      newNode = new node;
                      temp = newNode;
                      temp-&gt;x = 2;
                      temp-&gt;next = NULL;
                  }
              }
          }
      }
  }
  
  void Print() {
      while(head-&gt;next != NULL)
      {
          std::cout&lt;&lt; head-&gt;x &lt;&lt; std::endl;
          head=head-&gt;next;
      }
  }
  
  int main()
  {
      int input = 0;
      while (true) {
          std::cout &lt;&lt; "1. Add Node and Print " &lt;&lt; std::endl;
  
          std::cin &gt;&gt; input;
          switch ( input ) {
              case 1:
                  addNode(0, 5);
                  addNode(5, 5);
                  addNode(1, 5);
                  Print();
                  break;
              default:
                  std::cout&lt;&lt;"Bad Input";
                  break;
          }
          std::cin.get();
      }
      return 0;
  }
  </code></pre>