Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

DoublyLinkedList.java QUESTION Task 1. Please implement two functions, public void remove(Node currentNode) and public void insertAfter(Node currentNode, Node newNode) , for class DoublyLinkedList . The

DoublyLinkedList.java

QUESTION

Task 1. Please implement two functions, public void remove(Node currentNode) and public void insertAfter(Node currentNode, Node newNode), for class DoublyLinkedList. 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 two 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.

Main.java

public class Main { public static void main(String[] args) { LinkedList numList = new LinkedList(); Node nodeA = new Node(14); Node nodeB = new Node(2); Node nodeC = new Node(20); Node nodeD = new Node(31); Node nodeE = new Node(16); Node nodeF = new Node(55);

numList.append(nodeA); // Add 14 numList.append(nodeB); // Add 2, make the tail numList.append(nodeC); // Add 20, make the tail

numList.prepend(nodeD); // Add 31, make the head

numList.insertAfter(nodeB, nodeE); // Insert 16 after 2 numList.insertAfter(nodeC, nodeF); // Insert 55 after tail, 55 becomes new tail

// Output list System.out.print("List eles after inserting nodes: "); numList.printList();

// Remove the tail node, then the head node numList.remove(nodeF); numList.remove(nodeD); numList.remove(nodeB);

// Output final list System.out.print("List eles after removing nodes: "); numList.printList(); } } DoublyLinkedList.java

class Node { public int data; public Node next; public Node previous;

public Node(int initialData) { data = initialData; next = null; previous = 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; newNode.previous = tail; tail = newNode; } } public void prepend(Node newNode) { if (head == null) { head = newNode; tail = newNode; } else { newNode.next = head; head.previous = newNode; head = newNode; } } public void printList() { Node node = head; while (node != null) { System.out.print(node.data + " "); node = node.next; } System.out.println(); } public void insertAfter(Node currentNode, Node newNode) { // provide your implementation // Your implementation goes here. } public void remove(Node currentNode) { // provide your implementation // Your implementation goes here.

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data With Hadoop MapReduce A Classroom Approach

Authors: Rathinaraja Jeyaraj ,Ganeshkumar Pugalendhi ,Anand Paul

1st Edition

1774634848, 978-1774634844

More Books

Students also viewed these Databases questions

Question

What is heritability?

Answered: 1 week ago