program LinkedList;

type
  itemtype = integer;   {Contents of List-the items}

  nodePtr = ^listnode;  {a pointer to a node of the list}

  listnode = record     {List Node Structure}
    itemData: itemtype; {the data part within the node}
    next: nodePtr;      {a pointer to point to the next item}
  end;

  list = record         {List Structure}
    frontPtr : nodePtr; {a pointer to point at the first node}
    num : integer;      {the number of items in the list}
    backPtr : nodePtr;  {a pointer to point at the last node}
  end;


{*****************************************
Procedure to Initialise a List
Pre: List not initialised
Post:List initialised with first position
     ready to accept the first item.
******************************************}
procedure init(var l:list);
  begin
    l.frontPtr:= nil;   {points at nothing}
    l.backPtr := nil;   {points at nothing}
    l.num:=0;           {no items in the list}
  end;


{*****************************************
Function to return True/False if list is full
Pre: List initialised
Post:List unchanged
******************************************}
function isFull(l:list): boolean;
begin
isFull:=false;  {can never be full}
end;

{*****************************************
Function to return True/False if list is empty
Pre: List initialised
Post:List unchanged
******************************************}
function isEmpty(l:list): boolean;
begin
isEmpty:=l.num=0;       {if the number of items is 0}
end;


{*****************************************
Function to return number of items in List
Pre: List initialised
Post:List unchanged
******************************************}
function itemNo(l:list): integer;
begin
itemNo:=l.num;  {that part of the list which stores number of items}
end;


{*****************************************
Procedure to Insert an item in the List
Pre: List initialised
Post:List has item inserted
******************************************}
procedure insert(var l:list;i:itemtype);
  var
     tempPtr : nodePtr;

  begin
    new(tempPtr);               {}
    tempPtr^.itemData:=i;       {}
    tempPtr^.next:=nil;         {}
    if (l.num=0)                        {if it is the first item}
       then l.frontPtr:=tempPtr         {then set the front pointer}
       else l.backPtr^.next:=tempPtr;   {else set the last node to at new node}
    l.backPtr:=tempPtr;         {move the back pointer to point at new node}
    l.num:=l.num+1;             {increment the number of items}
  end;

{*****************************************
               MAIN PROGRAM
******************************************}
Var
  TestList:list;

BEGIN
  init(TestList);
  insert(TestList,21);
  insert(TestList,24);
  insert(TestList,28);
  writeln('Number of items in list: ',itemNo(TestList));
END.
