title: Only the last element of the linked list is displayed
body: <p>I have been trying to create a linked list in c++. But only the last element of the linked list is displayed. I have searched for the error but i cannot find it. I have implemented the logic that I learned form c language. All the nodes are connecting properly. But still i cannot find the error.
  This logic works on c language.
  Please help.</p>
  <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  
  class node{
  public:
      int data;
      node *next;
  }*head,*newnode,*temp;
  
  node* getnode();
  node* create(int);
  void display(node*);
  
  int main()
  {
      int n;
      head=getnode();
      cout&lt;&lt;&quot;Enter the no of nodes: &quot;;
      cin&gt;&gt;n;
      head=create(n);
      display(head);
      return 0;
  }
  
  node *getnode()
  {
      head=new node();
      head-&gt;next=nullptr;
      return(head);
  }
  
  node *create(int n)
  {
      head=getnode();
      cout&lt;&lt;&quot;Enter the value of node 1: &quot;;
      cin&gt;&gt;head-&gt;data;
      temp=getnode();
      temp=head;
      for(int i=1;i&lt;n;i++)
      {
          newnode=getnode();
          cout&lt;&lt;&quot;Enter the value of node &quot;&lt;&lt;i+1&lt;&lt;&quot;: &quot;;
          cin&gt;&gt;newnode-&gt;data;
          newnode-&gt;next=nullptr;
          temp-&gt;next=newnode;
          temp=newnode;
      }
      return(head);
  }
  
  void display(node *head)
  {
      while(head!=nullptr)
      {
          cout&lt;&lt;&quot;-&gt;&quot;&lt;&lt;head-&gt;data;
          head=head-&gt;next;
      }
  }
  </code></pre>