title: Inserting element in a linked list with Pascal
body: <p>I've seen some algorithms designed to append an elment at the end of a linked list here and browsing other website, then I wrote a small procedure that i believe it should append a given element at the end of the list but it doesn't seems to work.
  My question here is, why it doesn't work?</p>
  
  <p>I defined pointers and nodes as follows:</p>
  
  <pre><code> Pointer = ^Node;
  Node = record
      about : element;
      next : Pointer;
  end;
  </code></pre>
  
  <p>And the following procedure receives a linked list L and a q element that should be appended to the end of L</p>
  
  <p>First I define the record that I'll insert afterwards</p>
  
  <pre><code>var INS : Poniter;
  ........
  INS.about := q;
  </code></pre>
  
  <p>And the procedure goes as follows:</p>
  
  <pre><code>{temp := L}  {I'll use this for my attempt below }
  if L&lt;&gt;NIL then
  begin
    while L^.next&lt;&gt;NIL do
    begin 
    L:= L^.next;
    end;
    L^.next := INS;
    INS^.next := NIL; 
    {L:=temp;} {I'll explain this in my attempt below}
  end
  else
   begin
    L:= INS;
   end;
  </code></pre>
  
  <p>I also have a small procedure that prints all the elements of the linked list</p>
  
  <pre><code> procedure displayElements(L : pointer);
  
   begin
    while L &lt;&gt; nil do
    begin
     writeln(L^.about);
     L := L^.next;
     end
   end;
  </code></pre>
  
  <p>The problem: after i run de program it only displays the last two entries of the list.</p>
  
  <p>Conjecture about the problem: I believe it only shows the last two because when i run the procedure displayElements the pointer L is already one element before NIL -because i used the second algorithm-.</p>
  
  <p>Attempt to a solution: Alright, i think i need to put L back at the very first place so that when i use displayElements it get all the elements in the list. But how could I do that?, I tried what i commented above saving L in temp but it didn't work. </p>
  
  <p>Any ideas?. Thanks.</p>