Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Delete the Fifths Start with the List class provided below. Add a method called deleteFifth() that rewires the linked list in the following way: deletes

Delete the Fifths Start with the List class provided below. Add a method called deleteFifth() that rewires the linked list in the following way: deletes the all nodes in positions that are divisible by 5 (the fifth, tenth, fifteenth, and so forth). Requirements: Your code must pass through the list only once. You may not use any additional data structures (like array or ArrayList) to temporarily store data contained in the list.

***************************** public class List { private class Node { int value; Node next; Node(int val, Node n) { value = val; next = n; } Node(int val) { // Call the other (sister) constructor. this(val, null); } } private Node first; // list head public List() { first = null; } public boolean isEmpty() { return first == null; } public int size() { int count = 0; Node p = first; while (p != null) { // There is an element at p count ++; p = p.next; } return count; } public void add(int e) { if (isEmpty()) { first = new Node(e); } else { // Add to end of existing list Node current = first; // moving current reference to the ent of the list while(current.next!=null) current = current.next; current.next = new Node(e); } } public void add(int index, int e) { if (index < 0 || index > size()) { String message = String.valueOf(index); throw new IndexOutOfBoundsException(message); } // Index is at least 0 if (index == 0) { // New element goes at beginning first = new Node(e, first); return; } // Set a reference pred to point to the node that // will be the predecessor of the new node Node pred = first; for (int k = 1; k <= index - 1; k++) { pred = pred.next; } // Splice in a node containing the new element pred.next = new Node(e, pred.next); } public String toString() { StringBuilder strBuilder = new StringBuilder(); // Use p to walk down the linked list Node p = first; while (p != null) { strBuilder.append(p.value + " "); p = p.next; } return strBuilder.toString(); } public int remove(int index) { if (index < 0 || index >= size()) { String message = String.valueOf(index); throw new IndexOutOfBoundsException(message); } int element; // The element to return if (index == 0) { // Removal of first item in the list element = first.value; first = first.next; } else { // To remove an element other than the first, // find the predecessor of the element to // be removed. Node pred = first; // Move pred forward index - 1 times for (int k = 1; k <= index -1; k++) pred = pred.next; // Store the value to return element = pred.next.value; // Route link around the node to be removed pred.next = pred.next.next; } return element; } }

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

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

More Books

Students also viewed these Databases questions