Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program LinkedListImplementation;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- System.SysUtils;
- type
- pNode = ^Node;
- Node = record
- data : String;
- next : pNode;
- end;
- LinkedList = class
- private
- head : pNode;
- public
- destructor Destroy; override; //clears memory. destroys class.
- function peekPointer() : pNode; //gives the pointer of last node.
- function dumpList() : String; //gives linked list as a string.
- function findNode(data : String) : pNode; //gives pointer of node.
- function pop() : String; //removes last element.
- procedure push (data : String); //adds node to end of list.
- procedure deleteNode(data : String); //deletes node with findNode().
- procedure clear(); //clears memory.
- end;
- destructor LinkedList.Destroy; //note: is called automatically with class.Free
- begin
- clear;
- end;
- //note: the 'return' in pascal is a variable called 'result'. the main
- //difference being that it doesn't stop the function when assigned.
- function LinkedList.peekPointer : pNode;
- begin
- result := head;
- if result <> nil then
- begin
- while result.next <> nil do
- result := result.next;
- end;
- end;
- function LinkedList.dumpList : String;
- var currentNode : pNode;
- begin
- currentNode := head;
- result := '';
- if currentNode <> nil then
- begin
- result := currentNode^.data;
- while currentNode.next <> nil do
- begin
- currentNode := currentNode.next;
- result := result + ' -> ' + currentNode.data;
- end;
- end;
- end;
- function LinkedList.findNode(data : String) : pNode;
- var
- nodeFound : boolean;
- currentNode, foundNode : pNode;
- begin
- nodeFound := False;
- currentNode := head;
- result := nil;
- if currentNode <> nil then
- begin
- while currentNode <> nil do
- begin
- if currentNode^.data = data then
- begin
- nodeFound := True;
- foundNode := currentNode;
- end;
- currentNode := currentNode^.next;
- end;
- if nodeFound then
- result := foundNode;
- end;
- end;
- function LinkedList.pop : String;
- var lastNode, secondLastNode : pNode;
- begin
- lastNode := head;
- while lastNode^.next <> nil do
- begin
- secondLastNode := lastNode;
- lastNode := lastNode.next;
- end;
- result := lastNode^.data;
- if lastNode = head then
- head := nil
- else
- secondLastNode.next := nil;
- end;
- procedure LinkedList.push(data : String);
- var newNode : pNode;
- begin
- new(newNode);
- newNode^.next := nil;
- newNode^.data := data;
- if head = nil then
- head := newNode
- else
- peekPointer()^.next := newNode;
- end;
- procedure LinkedList.clear;
- var currentNode, nextNode : pNode;
- begin
- currentNode := head;
- while currentNode <> nil do
- begin
- nextNode := currentNode.next;
- Dispose(currentNode);
- currentNode := nextNode;
- end;
- end;
- procedure LinkedList.deleteNode(data : String);
- var previousNode, thisNode : pNode;
- begin
- thisNode := head;
- while (thisNode <> nil) and (thisNode^.data <> data) do
- begin
- previousNode := thisNode;
- thisNode := thisNode^.next;
- end;
- if thisNode <> nil then
- begin
- if thisNode = head then
- head := thisNode^.next
- else
- previousNode^.next := thisNode.next;
- end;
- Dispose(thisNode);
- end;
- var
- myList : LinkedList;
- closeMenu : Boolean;
- menuChoice : Char;
- nodeDataInput : String;
- begin
- try
- try
- myList := LinkedList.Create;
- closeMenu := False;
- repeat
- WriteLn('1 - Add node to list');
- WriteLn('2 - Pop node from list');
- WriteLn('3 - Delete node from list');
- WriteLn('4 - Print linked list');
- WriteLn('5 - Print last element');
- WriteLn('6 - Exit');
- WriteLn('');
- Write('Enter your choice (1 - 5): ');
- ReadLn(menuChoice);
- case menuChoice of
- '1' :
- begin
- Write('Enter data for new node: ');
- ReadLn(nodeDataInput);
- myList.push(nodeDataInput);
- end;
- '2' : WriteLn('Element popped: ' + myList.pop);
- '3' :
- begin
- Write('Enter data to delete: ');
- ReadLn(nodeDataInput);
- myList.deleteNode(nodeDataInput);
- end;
- '4' : WriteLn(myList.dumpList());
- '5' : WriteLn(myList.peekPointer()^.data);
- '6' : closeMenu := True;
- else WriteLn('Invalid choice');
- end;
- ReadLn;
- until closeMenu = True;
- finally
- myList.Free;
- end;
- except
- on E: Exception do
- Writeln(E.ClassName, ': ', E.Message);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement