Question
heres the LinkedList code: import java.util.*; public class LinkedList { public Node header; public LinkedList() { header = null; } public final Node Search(int key)
heres the LinkedList code:
import java.util.*;
public class LinkedList { public Node header;
public LinkedList() { header = null; }
public final Node Search(int key) { Node current = header; while (current != null && current.item != key) { current = current.link; } return current; }
public final void Append(int newItem) { Node newNode = new Node(newItem); newNode.link = header; header = newNode; }
public final Node Remove() { Node x = header; if (header != null) { header = header.link; } return x; }
public final Node searchPrevious(int key) { if (header == null) { return header; } else { Node current = header; while (!(current.link == null) && (current.link.item != key)) { current = current.link; } return current; } }
public final void Insert(int newItem, int preKey) { Node current; Node newNode = new Node(newItem); current = Search(preKey); if (current == null) { System.out.println("there is no such preKey!"); } else { newNode.link = current.link; current.link = newNode; } }
public final void Delete(int key) { if (header == null) // The list is empty! { System.out.println("The list is empty!"); } else { if (header.item == key) // header to be deleted. { header = header.link; } else { Node p = searchPrevious(key); if (p.link == null) { System.out.println("There is no such item!"); } else { p.link = p.link.link; } } } }
public final void ShowLinkedList() { if (header == null) System.out.println("The list is empty!"); else { Node current = header; System.out.printf("%1$s->", current.item); while (!(current.link == null)) { current = current.link; System.out.printf("%1$s->", current.item);
} System.out.printf("null"); System.out.println(); } } public final void PrintList() { if (header == null) { System.out.println("The list is empty!"); } else { Node current = header; System.out.println(current.item); while (!(current.link == null)) { current = current.link; System.out.println(current.item); } } } }
Use LinkedList as a reference, add the following operations in the class LinkedList; a) Find the average data values of the linked list. b) Find the node with largest key, and then delete the node. (Note, you must return the node, not just the largest key) c) Test above two operations in the Main method. (display the average of the data values of the linked list, the largest key, the linked list before and after deleting the node with the largest keyStep 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