Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program LinkedListImplementation;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- Node = record
- data: string;
- pNext: ^Node;
- end;
- type
- LinkedList = class
- pHead: ^Node;
- function peekLastNode (currentNode: Node) : Node;
- function listToString(currentNode: Node) : String;
- procedure addNode (newNode: Node);
- end;
- //the initial parameter for this function is LinkedList.pHead^
- function LinkedList.peekLastNode (currentNode: Node) : Node;
- begin
- if currentNode.pNext = nil then
- result := currentNode
- else
- result := peekLastNode(currentNode.pNext^);
- end;
- //produces string in form 'abc -> def -> ghi' from linked list
- function LinkedList.listToString(currentNode: Node) : String;
- begin
- if currentNode.pNext = nil then
- result := currentNode.data
- else
- result := currentNode.data + ' -> ' + listToString(currentNode.pNext^)
- end;
- //this uses helper method 'peekLastNode'
- procedure LinkedList.addNode(newNode: Node);
- var lastNode: Node;
- begin
- if pHead = nil then
- pHead := @newNode
- else
- lastNode := peekLastNode(pHead^);
- lastNode.pNext := @newNode;
- end;
- var
- Strings: LinkedList;
- String1: Node;
- String2: Node;
- begin
- try
- String1.data := 'abc';
- String2.data := 'def';
- Strings.Create();
- Strings.addNode(String1);
- Strings.addNode(String2);
- WriteLn(Strings.listToString(Strings.pHead^));
- ReadLn;
- except
- on E: Exception do
- Writeln(E.ClassName, ': ', E.Message);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement