Program List (Input,Output);

Type
   NodePointer = ^Node;
   Node = Record
           Item : Integer;
           Next : NodePointer;
          End;

Var
   List : NodePointer;
   Continue : Boolean;
   Val : Integer;
   Op : Char;




{*************************************************}
Procedure CreateList (Var List : NodePointer);

Var
   Current, NodePtr : NodePointer;
   Val : Integer;
   Continue : Boolean;

Begin
   New(List);
   Current := List;
   Writeln('Enter value for node item');
   Readln(Val);
   List^.Item := Val;

   Continue := True;
   While Continue Do
    Begin
      Writeln('Enter value for node item');
      Readln(Val);
      If Val >= 0 Then
       Begin
         New(NodePtr);
         NodePtr^.Item := Val;
         Current^.Next := NodePtr;
         Current := NodePtr;
       End
      Else
       Begin
         Current^.Next := Nil;
         Continue := False;
       End;
    End;
End;




{********************************************************}
Procedure OldInsert (Var List : NodePointer; Item : Integer);

Var
   Current, Previous, NewNodePtr : NodePointer;
   Found : Boolean;

Begin
   New(NewNodePtr);
   NewNodePtr^.Item := Item;

   If List = Nil Then    { Empty list }
    Begin
      List := NewNodePtr;
      NewNodePtr^.Next := Nil;
    End
   Else      { Insert into nonempty list }
    Begin
      If Item <= List^.Item Then       { insert in front }
       Begin
         NewNodePtr^.Next := List;
         List := NewNodePtr;
       End
      Else                           { insert somewhere }
       Begin
         Previous := List;      { first node }
         Current := Previous^.Next;   { second node }
         Found := False;
         While Not Found and (Current <> Nil) Do
          Begin
            If Item <= Current^.Item Then   { insert here }
               Found := True    { quit search }
            Else
             Begin     { move pointers to next node }
               Previous := Current;
               Current := Current^.Next;
             End;
          End;
         Previous^.Next := NewNodePtr;
         NewNodePtr^.Next := Current;
       End;
    End;
End;  { Insert }

 

{********************************************************}
Procedure Insert (Var List : NodePointer; Item : Integer);

Var
   Current, Previous, NewNodePtr : NodePointer;
   Found : Boolean;

Begin
   New(NewNodePtr);      { create new node }
   NewNodePtr^.Item := Item;  { load it with data }

   Current := List;

   Found := False;  
   While Not Found and (Current <> Nil) Do
    Begin
      If Item <= Current^.Item Then   { insert here }
         Found := True    { quit search }
      Else
       Begin     { move pointers to next node }
         Previous := Current;
         Current := Current^.Next;
       End;
    End;

   NewNodePtr^.Next := Current; { new node points to current }
   If Current = List Then   { empty list }
      List := NewNodePtr   { first node in list }
   Else
      Previous^.Next := NewNodePtr;  

End;  { Insert1 }





{********************************************************}
Procedure OldDelete (Var List : NodePointer; Item : Integer);

Var
   Current, Previous, Temp : NodePointer;
   Found : Boolean;

Begin
   If Item = List^.Item Then  { delete first node }
    Begin
      Temp := List;
      List := List^.Next;
    End
   Else        { delete elsewhere }
    Begin
      Previous := List;   { first node }
      Current := Previous^.Next;   { second node }
      Found := False;
      While Not Found and (Current <> Nil) Do
       Begin
         If Item = Current^.Item Then
          Begin
            Previous^.Next := Current^.Next;
            Found := True;
          End
         Else
          Begin
            Previous := Current;
            Current := Current^.Next;
          End;
       End;
    End;
End;




{********************************************************}
Procedure Delete (Var List : NodePointer; Item : Integer);

Var
   Current, Previous, Temp : NodePointer;
   Found : Boolean;

Begin
   Current := List;

   Found := False;  
   While Not Found and (Current <> Nil) Do
    Begin
      If Item = Current^.Item Then   { delete this one }
         Found := True    { quit search }
      Else
       Begin     { move pointers to next node }
         Previous := Current;
         Current := Current^.Next;
       End;
    End;

   If Found Then
    Begin
      If Current = List Then   
         List := List^.Next   { delete first node in list }
      Else
         Previous^.Next := Current^.Next;  
      Temp := Current;
      Dispose(Temp);
    End;

End;  { Delete }









{*********************************************************}
Procedure DisplayList (List : NodePointer);

Var
   Current : NodePointer;

Begin
   Current := List;
   While Current <> Nil Do
     Begin
       Writeln(Current^.Item);
       Current := Current^.Next;
     End;
End;





Begin

{   CreateList(List);  }

   Continue := True;
   While Continue Do
    Begin
      Writeln('Enter action Add, Delete, Print');
      Readln(Op);
      Case Op of
       'A','a' : 
          Begin
            Writeln('Enter value for node item');
            Readln(Val);
            Insert(List,Val)
          End;
       'D','d' : 
          Begin
            Writeln('Enter value of node to be deleted');
            Readln(Val);
            Delete(List,Val);
          End;
       'P','p' : DisplayList(List); 
      End;  { case }
    End;  { while }
  
End.
