Question
class Node { public int data; public Node next; public Node(int data) { this.data = data; this.next = null; } } class LinkedList { private
class Node { public int data; public Node next;
public Node(int data) { this.data = data; this.next = null; } }
class LinkedList { private Node head; private Node tail;
public LinkedList() { head = null; tail = null; }
public void append(Node newNode) { if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; tail = newNode; } }
public void prepend(Node newNode) { if (head == null) { head = newNode; tail = newNode; } else { newNode.next = head; head = newNode; } }
public void printList() { Node node = head; while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); }
public Node searchList(Node targetNode) { // This function is covered in our lecture. Refer to the lecture's Pseudo code // and provide your implementation // Your implementation goes here.
}
public void insertAfter(Node currentNode, Node newNode) { // This function is covered in our lecture. Refer to the lecture's Pseudo code // and provide your implementation // Your implementation goes here.
}
public void removeAfter(Node currentNode) { // This function is covered in our lecture. Refer to the lecture's Pseudo code // and provide your implementation // Your implementation goes here.
} } ------------------------------
Main.java
class Main { public static void main(String[] args) { LinkedList numList = new LinkedList(); Node nodeA = new Node(66); Node nodeB = new Node(99); Node nodeC = new Node(44); Node nodeD = new Node(95); Node nodeE = new Node(42); Node nodeF = new Node(17);
numList.append(nodeB); // Add 99 numList.append(nodeC); // Add 44, make the tail numList.append(nodeE); // Add 42, make the tail
numList.prepend(nodeA); // Add 66, make the head
// Test function insertAfter() numList.insertAfter(nodeC, nodeD); // Insert 95 after 44 numList.insertAfter(nodeE, nodeF); // Insert 17 after tail (42)
// Output list System.out.print("List eles after inserting nodes: "); numList.printList(); // Test function searchList() if (numList.searchList(nodeC) != null) System.out.println("Node " + nodeC.data + ": found!" ); else System.out.println("Node " + nodeC.data + ": not found!" ); // Test function removeAfter(). First remove the tail node, then the head node numList.removeAfter(nodeE); numList.removeAfter(null);
// Test function searchList() if (numList.searchList(nodeA) != null) System.out.println("Node " + nodeA.data + ": found!" ); else System.out.println("Node " + nodeA.data + ": not found!" );
if (numList.searchList(nodeF) != null) System.out.println("Node " + nodeF.data + ": found!" ); else System.out.println("Node " + nodeF.data + ": not found!" ); // Output final list System.out.print("List eles after removing nodes: "); numList.printList(); }
}
-----------------------------------
QUESTION
Task 1. Please implement three functions, public void removeAfter(Node currentNode), public void insertAfter(Node currentNode, Node newNode), and public Node searchList(Node target), for class SinglyLinkedList. The function signatures and hints are already provided. Please see the comments and provide your solution.
Task 2. Use the existing code provided in the main() function and test the three functions.
Note: Although you are not allowed to change or delete the existing code in main(), you can add more code as you want for test purposes.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started