Advertisement
nein_yards

Linked List Object Pascal / Delphi 2.2

May 30th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. program LinkedListImplementation;
  2. {$APPTYPE CONSOLE}
  3. {$R *.res}
  4. uses
  5. System.SysUtils;
  6.  
  7. type
  8. pNode = ^Node;
  9. Node = record
  10. data : String;
  11. next : pNode;
  12. end;
  13. LinkedList = class
  14. private
  15. head : pNode;
  16. public
  17. destructor Destroy; override; //clears memory. destroys class.
  18. function peekPointer() : pNode; //gives the pointer of last node.
  19. function dumpList() : String; //gives linked list as a string.
  20. function findNode(data : String) : pNode; //gives pointer of node.
  21. function pop() : String; //removes last element.
  22. procedure push (data : String); //adds node to end of list.
  23. procedure deleteNode(data : String); //deletes node with findNode().
  24. procedure clear(); //clears memory.
  25. end;
  26.  
  27. destructor LinkedList.Destroy; //note: is called automatically with class.Free
  28. begin
  29. clear;
  30. end;
  31. //note: the 'return' in pascal is a variable called 'result'. the main
  32. //difference being that it doesn't stop the function when assigned.
  33. function LinkedList.peekPointer : pNode;
  34. begin
  35. result := head;
  36. if result <> nil then
  37. begin
  38. while result.next <> nil do
  39. result := result.next;
  40. end;
  41. end;
  42.  
  43. function LinkedList.dumpList : String;
  44. var currentNode : pNode;
  45. begin
  46. currentNode := head;
  47. result := '';
  48. if currentNode <> nil then
  49. begin
  50. result := currentNode^.data;
  51. while currentNode.next <> nil do
  52. begin
  53. currentNode := currentNode.next;
  54. result := result + ' -> ' + currentNode.data;
  55. end;
  56. end;
  57. end;
  58.  
  59. function LinkedList.findNode(data : String) : pNode;
  60. var
  61. nodeFound : boolean;
  62. currentNode, foundNode : pNode;
  63. begin
  64. nodeFound := False;
  65. currentNode := head;
  66. result := nil;
  67. if currentNode <> nil then
  68. begin
  69. while currentNode <> nil do
  70. begin
  71. if currentNode^.data = data then
  72. begin
  73. nodeFound := True;
  74. foundNode := currentNode;
  75. end;
  76. currentNode := currentNode^.next;
  77. end;
  78. if nodeFound then
  79. result := foundNode;
  80. end;
  81. end;
  82.  
  83. function LinkedList.pop : String;
  84. var lastNode, secondLastNode : pNode;
  85. begin
  86. lastNode := head;
  87. while lastNode^.next <> nil do
  88. begin
  89. secondLastNode := lastNode;
  90. lastNode := lastNode.next;
  91. end;
  92. result := lastNode^.data;
  93. if lastNode = head then
  94. head := nil
  95. else
  96. secondLastNode.next := nil;
  97. end;
  98.  
  99. procedure LinkedList.push(data : String);
  100. var newNode : pNode;
  101. begin
  102. new(newNode);
  103. newNode^.next := nil;
  104. newNode^.data := data;
  105. if head = nil then
  106. head := newNode
  107. else
  108. peekPointer()^.next := newNode;
  109. end;
  110.  
  111. procedure LinkedList.clear;
  112. var currentNode, nextNode : pNode;
  113. begin
  114. currentNode := head;
  115. while currentNode <> nil do
  116. begin
  117. nextNode := currentNode.next;
  118. Dispose(currentNode);
  119. currentNode := nextNode;
  120. end;
  121. end;
  122.  
  123. procedure LinkedList.deleteNode(data : String);
  124. var previousNode, thisNode : pNode;
  125. begin
  126. thisNode := head;
  127. while (thisNode <> nil) and (thisNode^.data <> data) do
  128. begin
  129. previousNode := thisNode;
  130. thisNode := thisNode^.next;
  131. end;
  132. if thisNode <> nil then
  133. begin
  134. if thisNode = head then
  135. head := thisNode^.next
  136. else
  137. previousNode^.next := thisNode.next;
  138. end;
  139. Dispose(thisNode);
  140. end;
  141.  
  142. var
  143. myList : LinkedList;
  144. closeMenu : Boolean;
  145. menuChoice : Char;
  146. nodeDataInput : String;
  147. begin
  148. try
  149. try
  150. myList := LinkedList.Create;
  151. closeMenu := False;
  152. repeat
  153. WriteLn('1 - Add node to list');
  154. WriteLn('2 - Pop node from list');
  155. WriteLn('3 - Delete node from list');
  156. WriteLn('4 - Print linked list');
  157. WriteLn('5 - Print last element');
  158. WriteLn('6 - Exit');
  159. WriteLn('');
  160. Write('Enter your choice (1 - 5): ');
  161. ReadLn(menuChoice);
  162. case menuChoice of
  163. '1' :
  164. begin
  165. Write('Enter data for new node: ');
  166. ReadLn(nodeDataInput);
  167. myList.push(nodeDataInput);
  168. end;
  169. '2' : WriteLn('Element popped: ' + myList.pop);
  170. '3' :
  171. begin
  172. Write('Enter data to delete: ');
  173. ReadLn(nodeDataInput);
  174. myList.deleteNode(nodeDataInput);
  175. end;
  176. '4' : WriteLn(myList.dumpList());
  177. '5' : WriteLn(myList.peekPointer()^.data);
  178. '6' : closeMenu := True;
  179. else WriteLn('Invalid choice');
  180. end;
  181. ReadLn;
  182. until closeMenu = True;
  183.  
  184. finally
  185. myList.Free;
  186. end;
  187. except
  188. on E: Exception do
  189. Writeln(E.ClassName, ': ', E.Message);
  190. end;
  191. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement