Advertisement
nein_yards

Linked List Object Pascal / Delphi 1.0

May 29th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. program LinkedListImplementation;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. {$R *.res}
  6.  
  7. uses
  8. System.SysUtils;
  9. type
  10. Node = record
  11. data: string;
  12. pNext: ^Node;
  13. end;
  14. type
  15. LinkedList = class
  16. pHead: ^Node;
  17. function peekLastNode (currentNode: Node) : Node;
  18. function listToString(currentNode: Node) : String;
  19. procedure addNode (newNode: Node);
  20. end;
  21.  
  22. //the initial parameter for this function is LinkedList.pHead^
  23. function LinkedList.peekLastNode (currentNode: Node) : Node;
  24. begin
  25. if currentNode.pNext = nil then
  26. result := currentNode
  27. else
  28. result := peekLastNode(currentNode.pNext^);
  29. end;
  30.  
  31. //produces string in form 'abc -> def -> ghi' from linked list
  32. function LinkedList.listToString(currentNode: Node) : String;
  33. begin
  34. if currentNode.pNext = nil then
  35. result := currentNode.data
  36. else
  37. result := currentNode.data + ' -> ' + listToString(currentNode.pNext^)
  38. end;
  39.  
  40. //this uses helper method 'peekLastNode'
  41. procedure LinkedList.addNode(newNode: Node);
  42. var lastNode: Node;
  43. begin
  44. if pHead = nil then
  45. pHead := @newNode
  46. else
  47. lastNode := peekLastNode(pHead^);
  48. lastNode.pNext := @newNode;
  49. end;
  50.  
  51. var
  52. Strings: LinkedList;
  53. String1: Node;
  54. String2: Node;
  55. begin
  56. try
  57. String1.data := 'abc';
  58. String2.data := 'def';
  59. Strings.Create();
  60. Strings.addNode(String1);
  61. Strings.addNode(String2);
  62.  
  63. WriteLn(Strings.listToString(Strings.pHead^));
  64. ReadLn;
  65. except
  66. on E: Exception do
  67. Writeln(E.ClassName, ': ', E.Message);
  68. end;
  69. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement