home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / JOBPTR.ZIP / JOBPTR.PAS
Encoding:
Pascal/Delphi Source File  |  1988-01-26  |  9.8 KB  |  203 lines

  1. Program JobPointer;
  2.  
  3. { Author: David Bennett / Dave 1-24-88
  4. {
  5. { This program is a demonstration of how to use pointers in Turbo Pascal.
  6. { Unlike other languages like 'C' and Assembler that use pointers for just
  7. { about everything but the kitchen sink, the only real use for pointers in
  8. { Pascal is dynamic memory allocation. Dynamic memory allocation allows the
  9. { program to only reserve memory in the heap that is currently being occupied
  10. { by data. As the data is no longer needed the memory space can be freed to
  11. { be used for data that must be allocated later in the programs execution.
  12. { You may want to use pointers if you were writting a word processor, or a
  13. { text file manipulation utility. Pointers in Pascal are most widely used
  14. { in applications that deal with large volumes of text or data.
  15. {
  16. { This program uses the Linked List method of allocation. In this type of
  17. { logical array the next items starting address is stored in every record.
  18. { This format allows for quick insertion and deletion of records into the
  19. { any area of the array. You will notice that nowhere in this program is an
  20. { ARRAY type declared.
  21. }
  22.  
  23. Uses       { Remove these two lines for use with }
  24.   Crt;     { Turbo Pascal Version 3 and before   }
  25.  
  26. Type
  27.   JobIndex = Integer;               { A logical record postion  }
  28.   JobPtr   = ^JobRec;               { A pointer to a job record }
  29.   JobRec   = Record
  30.                Desc   : String[25]; { The job description       }
  31.                Salary : Real;       { The job salary            }
  32.                Next   : JobPtr;     { A pointer to the next job }
  33.              End;
  34.  
  35. Var
  36.   FirstJob,            { The first job in the list }
  37.   LastJob   : JobPtr;  { The next job in the list  }
  38.  
  39. { * This procedure Initalizes the pointers to the begining and ending records.
  40. {   NOTE: If you call this routine after allocating records you will obviously
  41. {         lose anyway to regain that memory. Only call this procedure once
  42. {         at the top of the program.
  43. }
  44. Procedure InitPtrs;
  45. Begin
  46.   FirstJob := Nil;           { No first job and }
  47.   LastJob  := Nil;           { No last job      }
  48. End;
  49.  
  50. { * This procedure creates a new job record.
  51. }
  52. Procedure CreateJob(Var NewJob : JobPtr);
  53. Begin
  54.   New(NewJob);                                          { Allocate space }
  55.   Write('Job Description : '); ReadLn(NewJob^.Desc);    { Get Desc       }
  56.   Write('    Salary      : '); ReadLn(NewJob^.Salary);  { Get Salary     }
  57.   NewJob^.Next := Nil;                                  { Not linked yet }
  58. End;
  59.  
  60. { * This procedure appends a record to the job list.
  61. }
  62. Procedure AppendJob(Var NewJob : JobPtr);
  63. Begin
  64.   If FirstJob = Nil Then        { If no jobs yet then                 }
  65.     FirstJob := NewJob          {   New job is the first job          }
  66.   Else                          { or else if there are jobs then      }
  67.     LastJob^.Next := NewJob;    {   Link current last job to new job  }
  68.   LastJob := NewJob;            { New job is the last job in the list }
  69. End;
  70.  
  71. { * This procedure finds the Nth logical record in the job list.
  72. {   If N is greater that the logical number of records in the job list then
  73. {   FoundJob will return as nil.
  74. }
  75. Procedure FindJob(N : JobIndex; Var FoundJob : JobPtr);
  76. Var
  77.   I : JobIndex;
  78. Begin
  79.   I := N;                                  { Set I equal to logical job #    }
  80.   FoundJob := FirstJob;                    { Set job pointer to first job    }
  81.   While (I>1) And                          { While theres more to look at &  }
  82.   Not (FoundJob = Nil) Do Begin            { Not at the end of the list      }
  83.     FoundJob := FoundJob^.Next;            {   Look at next record           }
  84.     I := I - 1;                            {   Decrement looks to make       }
  85.   End;
  86. End;
  87.  
  88. { * This procedure will delete N records from the job list starting with the
  89. {   logical record number S.
  90. }
  91. Procedure DeleteJob(N : JobIndex);
  92. Var
  93.   DelJob,                { Pointer to job to delete            }
  94.   PrevJob  : JobPtr;     { Pointer to job before job to delete }
  95. Begin
  96.   If (N = 1) Then Begin                { If Deleteing the first record then   }
  97.     DelJob   := FirstJob;              {   job to delete equals first job     }
  98.     FirstJob := FirstJob^.Next;        {   first job equals second job        }
  99.     Dispose(DelJob);                   {   Free the memory for this record    }
  100.   End                                  {                                      }
  101.   Else Begin                           { If not deleting the first job then   }
  102.     FindJob(N-1, PrevJob);             {   Find the job previous              }
  103.     If Not (PrevJob = Nil) Then Begin  {   If there is a previous job         }
  104.       DelJob := PrevJob^.Next;         {     Delete Job equals the next job   }
  105.       If Not (DelJob = Nil) Then Begin {     If there is a next job then      }
  106.         PrevJob^.Next := DelJob^.Next; {       Previous job gets it link      }
  107.         If (DelJob = LastJob) Then     {       If it's the last job then      }
  108.           LastJob := PrevJob;          {         The previous job is now last }
  109.         Dispose(DelJob);               {       Free memory for this job       }
  110.       End;
  111.     End;
  112.   End;
  113. End;
  114.  
  115. { * This procedure inserts a the job NewJob before the logical record N in the
  116. {   job list.
  117. }
  118. Procedure InsertJob(N : JobIndex; Var NewJob : JobPtr);
  119. Var
  120.   PrevJob : JobPtr;   { Pointer to job before instertion }
  121. Begin
  122.   If (N = 1) Then Begin               { If inserting before first record      }
  123.     NewJob^.Next := FirstJob;         {   The New Job links to current first  }
  124.     FirstJob     := NewJob;           {   The New job is now the first        }
  125.   End                                 {                                       }
  126.   Else Begin                          { If not inserting before 1st job then  }
  127.     FindJob(N-1, PrevJob);            {   Find the job before insert location }
  128.     If Not (PrevJob = Nil) Then Begin {   If there is a job before then       }
  129.       NewJob^.Next  := PrevJob^.Next; {     Link new job to job after         }
  130.       PrevJob^.Next := NewJob;        {     Link job before to new job        }
  131.     End;
  132.   End;
  133. End;
  134.  
  135. { === MAIN PROGRAM === }
  136.  
  137. Var
  138.   TheJob : JobPtr;       { Job pointer to pass to procedures }
  139.   I      : JobIndex;     { Logical job # holder              }
  140.   Ch     : Char;         { Character from user               }
  141.   L      : Integer;      { Loop variable                     }
  142.  
  143. Begin
  144.   ClrScr;                                                { Clear the display  }
  145.   WriteLn('Dynamic Memory Allocation Demo');             { Show program title }
  146.   WriteLn('Author: David Bennett / Date 1-24-88');       { Show my name/date  }
  147.   InitPtrs;                                              { reset pointers     }
  148.   Repeat                                                 { Top of the program }
  149.     WriteLn;                                             {               loop }
  150.     Write('A)ppend, D)elete, I)nsert, L)ist, Q)uit :');  { Show menu          }
  151.     ReadLn(Ch);                                          { Get response       }
  152.     Ch := UpCase(Ch);                                    { Force to uppercase }
  153.     WriteLn;
  154.  
  155.     { * Add job }
  156.     If Ch = 'A' Then Begin        { If user selected add then          }
  157.       CreateJob(TheJob);          {   Create the record                }
  158.       AppendJob(TheJob);          {   Append it to the end of the list }
  159.     End;
  160.  
  161.     { * Delete job }
  162.     If Ch = 'D' Then Begin                    { If user selected delete then  }
  163.       Write('Job # to delete : '); ReadLn(I); {   get the job to delete       }
  164.       FindJob(I, TheJob);                     {   look for the job            }
  165.       If Not (TheJob = Nil) Then DeleteJob(I) {     If found then delete      }
  166.       Else WriteLn('Invalid Job!');           {     Else show error msg       }
  167.     End;
  168.  
  169.     { * Insert job }
  170.     If Ch = 'I' Then Begin                   { If user selected insert then  }
  171.       Write('Insert before which job # : '); {   Get the job to              }
  172.       ReadLn(I);                             {        to delete              }
  173.       FindJob(I, TheJob);                    {   Find the job                }
  174.       If Not (TheJob = Nil) Then Begin       {   If found then               }
  175.         CreateJob(TheJob);                   {     Create the job record     }
  176.         InsertJob(I, TheJob);                {     Insert the job record     }
  177.       End                                    {                               }
  178.       Else WriteLn('Invalid Job!');          {   If not found show error msg }
  179.     End;
  180.  
  181.     { * List jobs }
  182.     If Ch = 'L' Then Begin
  183.       Write('List from job : '); ReadLn(I);                  { Get start job  }
  184.       FindJob(I, TheJob);                                    { Find start job }
  185.       If Not (TheJob = Nil) Then Begin                       { If valid job   }
  186.         L := I;                                              {   set loop     }
  187.         WriteLn('Job Description              Salary');      {   display the  }
  188.         WriteLn('----------------------------------------'); {   report head  }
  189.         Repeat
  190.           With TheJob^ Do
  191.             WriteLn(L:3,' ',                        { Display the job number  }
  192.                     Desc,' ':25-Length(Desc),       {            description  }
  193.                     Salary:8:2);                    {                 salary  }
  194.           TheJob := TheJob^.Next;                   { point to the next job   }
  195.           L := L + 1;                               { increment the loop      }
  196.         Until (TheJob = Nil);                       { repeat until none left  }
  197.       End
  198.       Else WriteLn('Invalid Job!');                     { If job was'nt valid }
  199.     End;
  200.  
  201.   Until (Ch = 'Q');   { Continue until user selects 'Q' for Quit }
  202. End {Program Pointer}.
  203.