Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Programming: I am currently stuck on a question and I know I am over thinking it. Any help is appreciated. c.) Write the following

Java Programming:

I am currently stuck on a question and I know I am over thinking it. Any help is appreciated.

c.) Write the following method addBefore() for the class KWSingleLinkedList without using helper methods including add(), addFirst(), addAfter(), remove(), removeFirst(), removeAfter(). You can use this.head to get head of a linked list, you can also use methods getNode(index), getHead() and setHead() if you need them.

Here is my code for reference:

public class KWSingleLinkedList { public void setSize(int size) { this.size = size; }

/** Reference to list head. */ private Node head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E item) { setHead(new Node<>(item, getHead())); size++; } /** Add a node after a given node @param node The node preceding the new item @param item The item to insert */ private void addAfter(Node node, E item) { node.next = new Node<>(item, node.next); size++; } /** Remove the node after a given node @param node The node before the one to be removed @return The data from the removed node, or null if there is no node to remove */ @SuppressWarnings("unused") private E removeAfter(Node node) { Node temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; } } /** Remove the first node from the list @return The removed node's data or null if the list is empty */ @SuppressWarnings("unused") private E removeFirst() { Node temp = getHead(); if (getHead() != null) { setHead(getHead().next); } // Return data at old head or null if list is empty if (temp != null) { size--; return temp.data; } else { return null; } } /** Find the node at a specified position @param index The position of the node sought @return The node at index or null if it does not exist */ private Node getNode(int index) { Node node = getHead(); for (int i = 0; i < index && node != null; i++) { node = node.next; } return node; } /** Get the data at index @param index The position of the data to return @return The data at index @throws IndexOutOfBoundsException if index is out of range */ public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); return node.data; } /** Store a reference to anEntry in the element at position index. @param index The position of the item to change @param newValue The new data @return The data previously at index @throws IndexOutOfBoundsException if index is out of range */ public E set(int index, E newValue) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node node = getNode(index); E result = node.data; node.data = newValue; return result; } /** Insert the specified item at index @param index The position where item is to be inserted @param item The item to be inserted @throws IndexOutOfBoundsException if index is out of range */ public void add(int index, E item) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node node = getNode(index-1); addAfter(node, item); } } /** Append item to the end of the list @param item The item to be appended @return true (as specified by the Collection interface) */ public boolean add(E item) { add(size, item); return true; } public Node getHead() { return head; }

public void setHead(Node head) { this.head = head; } public String toString() { @SuppressWarnings("unchecked") Node nodeRef = (Node) head; StringBuilder result = new StringBuilder(); while (nodeRef != null) { result.append(nodeRef.data); if (nodeRef.next != null) { result.append(" ==> "); } nodeRef = nodeRef.next; } return result.toString(); }

public void addBefore(int index, E item) { // Add your code here }

//////////////////////////////////////////////////////////////////////////////

/** A Node is the building block for a singlelinked list. */ public static class Node { // Data Fields /** The reference to the data. */ private E data; /** The reference to the next node. */ private Node next; // Constructors /** Creates a new node with a null next field. @param dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another node. @param dataItem The data stored @param nodeRef The node referenced by new node */ private Node(E dataItem, Node nodeRef) { data = dataItem; next = nodeRef; } } }

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_2

Step: 3

blur-text-image_3

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

Databases Illuminated

Authors: Catherine M. Ricardo

1st Edition

0763733148, 978-0763733148

More Books

Students also viewed these Databases questions