I'm currently try to implement one way linked list using pascal but i'm kinda lost , below i tried to create a list before adding a node then assign nil to the last node but then when i'm trying adding a new node i can't access it (runtime error 216) or that i get the last pointer value for some reasons i don't understand.
Program test;
type
TNodePtr = ^TNode;
TNode = record
number:Integer;
next:TNodePtr;
end;
var
head, tail, temp : TNodePtr;
node : TNode;
i : INTEGER;
procedure InitLists;
begin
head := nil ;
tail := head ;
end;
begin
//Initializing the list
InitLists;
new(head);
new(tail);
//creating the list
//first node
node.number := 1 ;
new(node.next);
head^ := node ;
tail^ := node ;
//second node
node.number := 2 ;
new(node.next);
tail^.next^ := node;
tail^ := node ;
//third node
node.number := 3 ;
node.next := nil;
tail^.next^ := node;
tail^ := node ;
//adding a node (forth node)
node.number := 4 ;
new(node.next);
new(tail^.next);
tail^.next^ := node ;
tail^ := node ;
//printing the list
temp := head ;
for i:=1 to 4 do
begin
writeln(temp^.number);
if temp^.next <> nil then
begin
temp := temp^.next ;
//this is for testing
//The final node is not being printed
//but instead i'm getting 3 which is the previous node value
writeln('done')
end
end
end.