home *** CD-ROM | disk | FTP | other *** search
- Program JobPointer;
-
- { Author: David Bennett / Dave 1-24-88
- {
- { This program is a demonstration of how to use pointers in Turbo Pascal.
- { Unlike other languages like 'C' and Assembler that use pointers for just
- { about everything but the kitchen sink, the only real use for pointers in
- { Pascal is dynamic memory allocation. Dynamic memory allocation allows the
- { program to only reserve memory in the heap that is currently being occupied
- { by data. As the data is no longer needed the memory space can be freed to
- { be used for data that must be allocated later in the programs execution.
- { You may want to use pointers if you were writting a word processor, or a
- { text file manipulation utility. Pointers in Pascal are most widely used
- { in applications that deal with large volumes of text or data.
- {
- { This program uses the Linked List method of allocation. In this type of
- { logical array the next items starting address is stored in every record.
- { This format allows for quick insertion and deletion of records into the
- { any area of the array. You will notice that nowhere in this program is an
- { ARRAY type declared.
- }
-
- Uses { Remove these two lines for use with }
- Crt; { Turbo Pascal Version 3 and before }
-
- Type
- JobIndex = Integer; { A logical record postion }
- JobPtr = ^JobRec; { A pointer to a job record }
- JobRec = Record
- Desc : String[25]; { The job description }
- Salary : Real; { The job salary }
- Next : JobPtr; { A pointer to the next job }
- End;
-
- Var
- FirstJob, { The first job in the list }
- LastJob : JobPtr; { The next job in the list }
-
- { * This procedure Initalizes the pointers to the begining and ending records.
- { NOTE: If you call this routine after allocating records you will obviously
- { lose anyway to regain that memory. Only call this procedure once
- { at the top of the program.
- }
- Procedure InitPtrs;
- Begin
- FirstJob := Nil; { No first job and }
- LastJob := Nil; { No last job }
- End;
-
- { * This procedure creates a new job record.
- }
- Procedure CreateJob(Var NewJob : JobPtr);
- Begin
- New(NewJob); { Allocate space }
- Write('Job Description : '); ReadLn(NewJob^.Desc); { Get Desc }
- Write(' Salary : '); ReadLn(NewJob^.Salary); { Get Salary }
- NewJob^.Next := Nil; { Not linked yet }
- End;
-
- { * This procedure appends a record to the job list.
- }
- Procedure AppendJob(Var NewJob : JobPtr);
- Begin
- If FirstJob = Nil Then { If no jobs yet then }
- FirstJob := NewJob { New job is the first job }
- Else { or else if there are jobs then }
- LastJob^.Next := NewJob; { Link current last job to new job }
- LastJob := NewJob; { New job is the last job in the list }
- End;
-
- { * This procedure finds the Nth logical record in the job list.
- { If N is greater that the logical number of records in the job list then
- { FoundJob will return as nil.
- }
- Procedure FindJob(N : JobIndex; Var FoundJob : JobPtr);
- Var
- I : JobIndex;
- Begin
- I := N; { Set I equal to logical job # }
- FoundJob := FirstJob; { Set job pointer to first job }
- While (I>1) And { While theres more to look at & }
- Not (FoundJob = Nil) Do Begin { Not at the end of the list }
- FoundJob := FoundJob^.Next; { Look at next record }
- I := I - 1; { Decrement looks to make }
- End;
- End;
-
- { * This procedure will delete N records from the job list starting with the
- { logical record number S.
- }
- Procedure DeleteJob(N : JobIndex);
- Var
- DelJob, { Pointer to job to delete }
- PrevJob : JobPtr; { Pointer to job before job to delete }
- Begin
- If (N = 1) Then Begin { If Deleteing the first record then }
- DelJob := FirstJob; { job to delete equals first job }
- FirstJob := FirstJob^.Next; { first job equals second job }
- Dispose(DelJob); { Free the memory for this record }
- End { }
- Else Begin { If not deleting the first job then }
- FindJob(N-1, PrevJob); { Find the job previous }
- If Not (PrevJob = Nil) Then Begin { If there is a previous job }
- DelJob := PrevJob^.Next; { Delete Job equals the next job }
- If Not (DelJob = Nil) Then Begin { If there is a next job then }
- PrevJob^.Next := DelJob^.Next; { Previous job gets it link }
- If (DelJob = LastJob) Then { If it's the last job then }
- LastJob := PrevJob; { The previous job is now last }
- Dispose(DelJob); { Free memory for this job }
- End;
- End;
- End;
- End;
-
- { * This procedure inserts a the job NewJob before the logical record N in the
- { job list.
- }
- Procedure InsertJob(N : JobIndex; Var NewJob : JobPtr);
- Var
- PrevJob : JobPtr; { Pointer to job before instertion }
- Begin
- If (N = 1) Then Begin { If inserting before first record }
- NewJob^.Next := FirstJob; { The New Job links to current first }
- FirstJob := NewJob; { The New job is now the first }
- End { }
- Else Begin { If not inserting before 1st job then }
- FindJob(N-1, PrevJob); { Find the job before insert location }
- If Not (PrevJob = Nil) Then Begin { If there is a job before then }
- NewJob^.Next := PrevJob^.Next; { Link new job to job after }
- PrevJob^.Next := NewJob; { Link job before to new job }
- End;
- End;
- End;
-
- { === MAIN PROGRAM === }
-
- Var
- TheJob : JobPtr; { Job pointer to pass to procedures }
- I : JobIndex; { Logical job # holder }
- Ch : Char; { Character from user }
- L : Integer; { Loop variable }
-
- Begin
- ClrScr; { Clear the display }
- WriteLn('Dynamic Memory Allocation Demo'); { Show program title }
- WriteLn('Author: David Bennett / Date 1-24-88'); { Show my name/date }
- InitPtrs; { reset pointers }
- Repeat { Top of the program }
- WriteLn; { loop }
- Write('A)ppend, D)elete, I)nsert, L)ist, Q)uit :'); { Show menu }
- ReadLn(Ch); { Get response }
- Ch := UpCase(Ch); { Force to uppercase }
- WriteLn;
-
- { * Add job }
- If Ch = 'A' Then Begin { If user selected add then }
- CreateJob(TheJob); { Create the record }
- AppendJob(TheJob); { Append it to the end of the list }
- End;
-
- { * Delete job }
- If Ch = 'D' Then Begin { If user selected delete then }
- Write('Job # to delete : '); ReadLn(I); { get the job to delete }
- FindJob(I, TheJob); { look for the job }
- If Not (TheJob = Nil) Then DeleteJob(I) { If found then delete }
- Else WriteLn('Invalid Job!'); { Else show error msg }
- End;
-
- { * Insert job }
- If Ch = 'I' Then Begin { If user selected insert then }
- Write('Insert before which job # : '); { Get the job to }
- ReadLn(I); { to delete }
- FindJob(I, TheJob); { Find the job }
- If Not (TheJob = Nil) Then Begin { If found then }
- CreateJob(TheJob); { Create the job record }
- InsertJob(I, TheJob); { Insert the job record }
- End { }
- Else WriteLn('Invalid Job!'); { If not found show error msg }
- End;
-
- { * List jobs }
- If Ch = 'L' Then Begin
- Write('List from job : '); ReadLn(I); { Get start job }
- FindJob(I, TheJob); { Find start job }
- If Not (TheJob = Nil) Then Begin { If valid job }
- L := I; { set loop }
- WriteLn('Job Description Salary'); { display the }
- WriteLn('----------------------------------------'); { report head }
- Repeat
- With TheJob^ Do
- WriteLn(L:3,' ', { Display the job number }
- Desc,' ':25-Length(Desc), { description }
- Salary:8:2); { salary }
- TheJob := TheJob^.Next; { point to the next job }
- L := L + 1; { increment the loop }
- Until (TheJob = Nil); { repeat until none left }
- End
- Else WriteLn('Invalid Job!'); { If job was'nt valid }
- End;
-
- Until (Ch = 'Q'); { Continue until user selects 'Q' for Quit }
- End {Program Pointer}.