Linked List

FlamingClaw 0 Tallied Votes 129 Views Share

see this simple example...very nice,and easy

(*Linked list 2009*)
(*This program will convert Dec To Bin*)
Program LinkedListByFlamingClaw;
Uses Crt;

Type Tptr=^RF;
     RF=Record
              DataField:Byte;
              MemoryAddress:Tptr;
        End;

Var A:Byte;{0..255}
   First,Second:Tptr;{pointer type}



Begin
     ClrScr; {Clear the screen..thx for the Crt unit :) }
     Write('Give me the number from 0 to 255: '); {the question}
     {$I-}    {checking out}
     ReadLn(A);    {catch it}
     {$I+}    {checking on}
     If IoResult <> 0 Then  {if IoResult not coming back with zero then exit}
        Begin
            Write('Wrong data!');
            ReadLn;
            Halt;
        End;
     First:=Nil;{it is points nowhere}
     While (A <> 0) Do {Analize it,while the condition is true...}
           Begin
                New(Second);  {create a new record}
                Second^.DataField:=A Mod 2;{fill the fileld with the remainder}
                Second^.MemoryAddress:=First; {the address will be the First's address}
                First:=Second; {First equal with the new place}
                A:=A Div 2; {divide by 2 without reaminder}
           End;
      {When it's done,we'll going back one by one
       and write that number to the screen}
     Second:=First;
     While Second<>Nil do
           Begin
                Write(Second^.DataField);
                Second:=Second^.MemoryAddress;
           End;
     {Ok everything went fine...now destroy those records from the memory}
     Second:=First;
     While(Second <> Nil) Do
                  Begin
                       First:=Second^.MemoryAddress;{addressing it}
                       Dispose(Second);  {destroy}
                       Second:=First;  {Second will be First}
                  End;

     ReadKey;
End.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.