Created
January 4, 2016 12:12
-
-
Save miklund/ca8a8983ea7256780a0e to your computer and use it in GitHub Desktop.
2011-02-19 ArrayList by units and objects in Pascal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Title: ArrayList by units and objects in Pascal | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2011/02/19/arraylist-by-units-and-objects-in-pascal.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unit Collections; | |
interface | |
type | |
(* Linked List *) | |
ListRef = ^List; | |
List = record | |
current : Pointer; (* Some object *) | |
next : ListRef; | |
end; | |
(* Array List Object *) | |
ArrayListRef = ^ArrayList; | |
ArrayList = object | |
first : ListRef; | |
last : ListRef; | |
constructor Init; | |
destructor Done; | |
procedure Add(el : Pointer); | |
function Empty : boolean; | |
function Head : Pointer; | |
function Tail : ArrayListRef; | |
end; | |
implementation | |
constructor ArrayList.Init; | |
begin | |
first := nil; | |
last := nil; | |
end; | |
(* Free all elements from the linked list *) | |
procedure DisposeAll(list : ListRef); | |
var next : ListRef; | |
begin | |
if list <> nil then begin | |
next := list^.next; | |
Dispose(list); (* Free memory *) | |
DisposeAll(next); | |
end | |
end; | |
destructor ArrayList.Done; | |
begin | |
last := nil; | |
DisposeAll(first); | |
first := nil; | |
end; | |
(* Add element to the list *) | |
procedure ArrayList.Add(el : Pointer); | |
var item : ListRef; | |
begin | |
New(item); | |
item^.current := el; | |
if first = nil then | |
first := item | |
else | |
last^.next := item; | |
last := item; | |
end; | |
(* Is the list empty? *) | |
function ArrayList.Empty : boolean; | |
begin | |
Empty := first = nil; | |
end; | |
(* First element in list *) | |
function ArrayList.Head : Pointer; | |
begin | |
if Empty then | |
Head := nil | |
else | |
Head := first^.current | |
end; | |
(* Rest of the list *) | |
function ArrayList.Tail : ArrayListRef; | |
var list : ArrayListRef; | |
begin | |
New(list, Init); | |
list^.first := first^.next; | |
list^.last := last; | |
Tail := list; | |
end; | |
end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program RecordStore; | |
uses Collections; (* Import the type ArrayList *) | |
type | |
RecordRef = ^RecordObject; | |
RecordObject = object | |
title : string; | |
artist : string; | |
constructor Create(titleInput, artistInput : string); | |
procedure Print; | |
end; | |
var records : ArrayListRef; | |
(* RecordObject instance procedures *) | |
constructor RecordObject.Create(titleInput, artistInput : string); | |
begin | |
title := titleInput; | |
artist := artistInput; | |
end; | |
procedure RecordObject.Print; | |
begin | |
Write(title); | |
Write(' - '); | |
Writeln(artist); | |
end; | |
(* Main program *) | |
procedure Add(title, artist : string); | |
var my_record : RecordRef; | |
begin | |
New(my_record, Create(title, artist)); | |
records^.Add(my_record); | |
end; | |
(* Go through list and print every record *) | |
procedure Print(list : ArrayListRef); | |
var | |
tail : ArrayListRef; | |
current : RecordRef; | |
begin | |
if not list^.Empty then begin | |
current := list^.Head; | |
current^.Print; (* Print record *) | |
tail := list^.Tail; | |
Print(tail); | |
Dispose(tail); (* Disposes the tail, not the records in the list *) | |
end; | |
end; | |
begin | |
(* Create new list of records *) | |
New(records, Init); | |
Add('Dark side of the moon', 'Pink Floyd'); | |
Add('The Rise and Fall of Ziggy Stardust and the Spiders from Mars', 'David Bowie'); | |
Add('L.A. Woman', 'The Doors'); | |
Print(records); | |
Dispose(records, Done); | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment