Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get().
import java.util.AbstractList;
public class SinglyLinkedList<E> extends AbstractList<E> {
/** Reference to the first node in our linked list. This will be null if the list is empty. */
private Entry head;
/**
* Creates an empty list.
*/
public SinglyLinkedList() {
reset();
}
/**
* This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This
* call will reset the head to be null, which also empties the linked list.
*/
private void reset() {
head = null;
}
/**
* Returns the element at the specified index in this list.
*
* @param index List location whose element should be returned.
* @return Element at the specified index in this list
*/
@Override
public E get(int index) {
}
/**
* Returns the number of elements currently in this list.
*
* @return the number of elements in the list
*/
@Override
public int size() {
}
/**
* Class which defines the Entry instances ("nodes") in this single-linked-based list. Note that this class does not
* specify a generic type, because it MUST be identical to the element type of the main class.
*
* @author Matthew Hertz
*/
private class Entry {
/** Element stored with the current entry. */
private E element;
/** Reference to the next entry in our linked list or null if this is the final link. */
private Entry next;
/** Create a new, blank Entry. */
public Entry() {
element = null;
next = null;
}
}
}
Please find my implementation.
package year_2017.october.thirdweek;
import java.util.AbstractList;
public class SinglyLinkedList<E> extends AbstractList<E> {
/** Reference to the first node in our linked list. This will be null if the list is empty. */
private Entry head;
/**
* Creates an empty list.
*/
public SinglyLinkedList() {
reset();
}
/**
* This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This
* call will reset the head to be null, which also empties the linked list.
*/
private void reset() {
head = null;
}
/**
* Returns the element at the specified index in this list.
*
* @param index List location whose element should be returned.
* @return Element at the specified index in this list
*/
@Override
public E get(int index) {
if(index < 0 || index >= size())
return null;
int i = 0;
Entry temp = head;
while(i < index) {
i++;
temp = temp.next;
}
return temp.element;
}
/**
* Returns the number of elements currently in this list.
*
* @return the number of elements in the list
*/
@Override
public int size() {
int i = 0;
Entry temp = head;
while(temp != null) {
i++;
temp = temp.next;
}
return i;
}
/**
* Class which defines the Entry instances ("nodes") in this single-linked-based list. Note that this class does not
* specify a generic type, because it MUST be identical to the element type of the main class.
*
* @author Matthew Hertz
*/
private class Entry {
/** Element stored with the current entry. */
private E element;
/** Reference to the next entry in our linked list or null if this is the final link. */
private Entry next;
/** Create a new, blank Entry. */
public Entry() {
element = null;
next = null;
}
}
}
Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E>...
Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...
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...
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 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...
Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue and LinkedQueue: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add N Integers to the structure and how long it takes to remove N Integers from the structure. N should vary from 10 to 100,000,000 increasing N by a factor of 10 for each test. Depending...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> 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...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...