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>...
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...
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...
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...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
create a class named IntegerQueue given a singlylinkedqueue of integers, write the following methods: max(SinglyLinkedQueue<Integer> s) to return the max element in the queu. min(SinglyLinkedQueue<Integer> s) to return the min element in the queue. sum(SinglyLInkedQueue<Integer> s) to return the sum of elements in the queu. median(SinglyLinkedQueue<Integer> s) to return the median of elements in the queue. split(SinglyLinkedQueue<Integer> s) to separate the SinglyLinkedQueue into two ArrayQueues based on whether the element values are even or odd. package Stack_and_Queue; import java.util.Iterator; import...