Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java:
1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T data)
2. Implement 4 remove() methods. One that will remove from the front (must be O(1)), one that will remove from the back (must be O(n)), one that will remove all occurences of a specific value from the list (must be O(n)), one that will remove at a particular given index (must be O(1) for index 0 and O(n) for all other indices). They are: T removeAtIndex(int index), T removeFromFront(), T removeFromBack, boolean removeAllOccurrences(T data)
3. Implement a method to get the element at a given index (must be O(1) for index 0 and O(n) for all other indices), return the list in the form of an array (must be O(n)), check if the given list is empty (must be O(1)), size of the list (must be O(1)), and method to clear the list (must be O(1)). They are: T get(int index), Object[] toArray(), boolean isEmpty(), int size(), void clear()
You are profided the interface for the Linked List called LinkedListInterface and the class for Linked List Node called LInkedListNode. The code for those two are below:
public interface LinkedListInterface {
/**
* Adds the element to the index
specified.
* Adding to indices 0 and {@code size}
should be O(1), all other adds are
* O(n).
*
* @param index The index where you want
the new element.
* @param data Any object of type T.
* @throws
java.lang.IndexOutOfBoundsException if index is negative
* or index > size.
* @throws
java.lang.IllegalArgumentException if data is null.
*/
public void addAtIndex(int index, T data);
/**
* Add a new node to the front of your
linked list that holds the given
* data. Make sure to update head.
*
* Must be O(1).
*
* @param data The data that the new node
should hold.
* @throws
java.lang.IllegalArgumentException if data is null.
*/
public void addToFront(T data);
/**
* Add a new node to the back of your
linked list that holds the given data.
* Make sure to update tail.
*
* Must be O(1).
*
* @param data The data that the new node
should hold.
* @throws
java.lang.IllegalArgumentException if data is null.
*/
public void addToBack(T data);
/**
* Returns the element at the given
index.
* This method must be O(1) for indices 0
and {@code size - 1}.
* O(n) is expected for all other
indices.
*
* @param index The index of the
element
* @return The object stored at that
index.
* @throws
java.lang.IndexOutOfBoundsException if index < 0 or
* index >= size.
*/
public T get(int index);
/**
* Removes and returns the element at
index.
* This method should be O(1) for index 0,
and O(n) in
* all other cases.
*
* @param index The index of the
element
* @return The object that was formerly at
that index.
* @throws
java.lang.IndexOutOfBoundsException if index < 0 or
* index >= size.
*/
public T removeAtIndex(int index);
/**
* Remove the front node from the list and
return the data from it. If the
* list is empty, return {@code
null}.
*
* Must be O(1).
*
* @return The data from the front node or
null.
*/
public T removeFromFront();
/**
* Remove the back node from the list and
return the data from it. If the
* list is empty, return {@code
null}.
*
* Must be O(n).
*
* @return The data from the last node or
null.
*/
public T removeFromBack();
/**
* Remove all occurrences of data in the
linked list.
*
* Must be O(n).
*
* @param data The data to be removed from
the list.
* @return true if something was removed
from the list; otherwise, false
* @throws
java.lang.IllegalArgumentException if data is null.
*/
public boolean removeAllOccurrences(T data);
/**
* Return the linked list represented as an
array of objects.
*
* Must be O(n).
*
* @return An array of length {@code size}
holding each element in the same
* order as it is in the linked list.
*/
public Object[] toArray();
/**
* Return a boolean value representing
whether or not the list is empty.
*
* Must be O(1).
*
* @return True if empty. False
otherwise.
*/
public boolean isEmpty();
/**
* Return the size of the list as an
integer.
*
* Must be O(1).
*
* @return The size of the list.
*/
public int size();
/**
* Clear the list.
*
* Must be O(1).
*/
public void clear();
/**
* Reference to the head node of the linked
list.
* Normally, you would not do this, but we
need it for grading your work.
*
* DO NOT USE THIS METHOD IN YOUR
CODE.
*
* @return Node representing the head of
the linked list.
*/
public LinkedListNode getHead();
/**
* Reference to the tail node of the linked
list.
* Normally, you would not do this, but we
need it for grading your work.
*
* DO NOT USE THIS METHOD IN YOUR
CODE.
*
* @return Node representing the tail of
the linked list.
*/
public LinkedListNode getTail();
}
public class LinkedListNode {
private T data;
private LinkedListNode next;
/**
* Create a new LinkedListNode with the
given data object and next node.
*
* @param data data to store in the
node
* @param next next node
*/
public LinkedListNode(T data, LinkedListNode
next) {
this.data = data;
this.next = next;
}
/**
* Create a new LinkedListNode with the
given data object and no next node.
*
* @param data data to store in this
node
*/
public LinkedListNode(T data) {
this(data, null);
}
/**
* Get the data stored in the node.
*
* @return data in this node.
*/
public T getData() {
return data;
}
/**
* Get the next node.
*
* @return next node.
*/
public LinkedListNode getNext() {
return next;
}
/**
* Set the next node.
*
* @param next new next node.
*/
public void setNext(LinkedListNode next) {
this.next = next;
}
@Override
public String toString() {
return "Node containing:
" + data;
}
}
Fill in your code in the given class below (this will be the answer):
public class SinglyLinkedList<T> implements LinkedListInterface<T> {
// Do not add new instance variables.
private LinkedListNode<T> head;
private LinkedListNode<T> tail;
private int size;
@Override
public void addAtIndex(int index, T data) {
}
@Override
public T get(int index) {
}
@Override
public T removeAtIndex(int index) {
}
@Override
public void addToFront(T data) {
}
@Override
public void addToBack(T data) {
}
@Override
public T removeFromFront() {
}
@Override
public T removeFromBack() {
}
@Override
public boolean removeAllOccurrences(T data)
{
}
@Override
public Object[] toArray() {
}
@Override
public boolean isEmpty() {
}
@Override
public int size() {
}
@Override
public void clear() {
}
@Override
public LinkedListNode<T> getHead() {
// DO NOT MODIFY!
return head;
}
@Override
public LinkedListNode<T> getTail() {
// DO NOT MODIFY!
return tail;
}
}
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...
Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...
Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...
package model; import java.util.Iterator; /** * This class implements interface PriorityList<E> to represent a generic * collection to store elements where indexes represent priorities and the * priorities can change in several ways. * * This collection class uses an Object[] data structure to store elements. * * @param <E> The type of all elements stored in this collection */ // You will have an error until you have have all methods // specified in interface PriorityList included inside this...
Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...
Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable { // ---------------- nested Node class...
In Java The following Java implementation of a class Node is given: private class Node<Object> { Node() { this(null, null); } Node(Object d) { this(d, null); } Node(Object d, Node n) { data = d; next = n; } Object data; Node next; } Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a reference to the header node. Using the class Node described above, write a MySingleLinkedList...
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...
In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...
Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...