1. import java.util.Iterator;
import java.util.NoSuchElementException;
import lab4.util.List;
public class SinglyLinkedList<E> implements List<E> {
@SuppressWarnings("hiding")
private class SinglyLinkedListIterator<E> implements Iterator<E>{
private Node<E> nextNode;
@SuppressWarnings("unchecked")
public SinglyLinkedListIterator() {
this.nextNode = (Node<E>) header.getNext();
}
@Override
public boolean hasNext() {
return nextNode != null;
}
@Override
public E next() {
if (this.hasNext()) {
E result = this.nextNode.getElement();
this.nextNode = this.nextNode.getNext();
return result;
}
else {
throw new NoSuchElementException();
}
}
}
private static class Node<E> {
private E element;
private Node<E> next;
public Node(E element, Node<E> next) {
super();
this.element = element;
this.next = next;
}
public Node() {
2. super();
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
private Node<E> header;
private int currentSize;
public SinglyLinkedList() {
this.header = new Node<>();
this.currentSize = 0;
}
@Override
public int size() {
return this.currentSize;
}
@Override
public boolean isEmpty() {
return this.size() == 0;
}
@Override
public boolean contains(E e) {
return this.firstIndexOf(e) >= 0;
}
@Override
public int firstIndexOf(E e) {
int i = 0;
3. for (Node<E> temp = this.header.getNext(); temp != null;
temp = temp.getNext(), ++i) {
if (temp.getElement().equals(e)) {
return i;
}
}
// not found
return -1;
}
@Override
public void add(E e) {
if (this.isEmpty()) {
this.header.setNext(new Node<E>(e, null));
this.currentSize++;
}
else {
Node<E>temp= this.header.getNext();
while (temp.getNext() != null) {
temp = temp.getNext();
}
Node<E> newNode = new Node<>(e, null);
temp.setNext(newNode);
this.currentSize++;
}
}
@Override
public void add(E e, int index) {
if ((index < 0) || (index > this.currentSize)) {
throw new IndexOutOfBoundsException();
}
if (index == this.currentSize) {
this.add(e);
}
else {
Node<E> temp = null;
if (index == 0) {
temp = this.header;
}
else {
temp = this.getPosition(index -1);
}
Node<E> newNode = new Node<>();
newNode.setElement(e);
4. newNode.setNext(temp.getNext());
temp.setNext(newNode);
this.currentSize++;
}
}
@Override
public E get(int position) {
if ((position < 0) || position >= this.currentSize) {
throw new IndexOutOfBoundsException();
}
Node<E> temp = this.getPosition(position);
return temp.getElement();
}
private Node<E> getPosition(int index){
int currentPosition=0;
Node<E> temp = this.header.getNext();
while(currentPosition != index) {
temp = temp.getNext();
currentPosition++;
}
return temp;
}
@Override
public boolean remove(int index) {
if ((index < 0) || (index >= this.currentSize)){
throw new IndexOutOfBoundsException();
}
else {
Node<E> temp = this.header;
int currentPosition =0;
Node<E> target = null;
while (currentPosition != index) {
temp = temp.getNext();
currentPosition++;
}
target = temp.getNext();
temp.setNext(target.getNext());
target.setElement(null);
5. target.setNext(null);
this.currentSize--;
return true;
}
}
@Override
public E set(int position, E newElement) {
if ((position < 0) || position >= this.currentSize) {
throw new IndexOutOfBoundsException();
}
Node<E> temp = this.getPosition(position);
E result = temp.getElement();
temp.setElement(newElement);
return result;
}
@Override
public void clear() {
while(!this.isEmpty()) {
this.remove(0);
}
}
@Override
public Iterator<E> iterator() {
return new SinglyLinkedListIterator<E>();
}
@Override
public int lastIndexOf(E e) {
int i = 0, result = -1;
for (Node<E> temp = this.header.getNext(); temp != null;
temp = temp.getNext(), ++i) {
if (temp.getElement().equals(e)) {
result = i;
}
}
// not found
return result;
}
6. @Override
public E first() {
return get(0);
}
@Override
public E last() {
return get(size() - 1);
}
@Override
public boolean remove(E e) {
int i = this.firstIndexOf(e);
if (i < 0) {
return false;
}else {
this.remove(i);
return true;
}
}
@Override
public int removeAll(E e) {
int count = 0;
while (this.remove(e)) {
count++;
}
return count;
}
/**
* TODO EXERCISE 1:
* Implement an O(n) member method called reverse()
* which reverses the elements in a list with n elements.
*
* For example, if L = {Ken, Al, Bob, Mel} then a call to L.reverse() turns L into
* L = {Mel, Bob, Al, Ken}.
*
* Note: A call to reverse() modifies the contents of the list L, it does not
* create a copy of L.
*/
7. public void reverse() {
/*TODO ADD YOUR CODE HERE*/
}
}
5. (20 pts) Head over to Hasprefixsumproblem. java after finishing exercise 4. The instructions
for this exercise are as follows: - Implement a method that determines if a LinkedList has an
initial sequence of nodes whose values sum to n. If so, it returns an integer corresponding to how
many elements at the beginning of the list add up to n . - The method receives as parameter a
Node that represents the head node of a singlyLinkedList, as well as an integer n denoting a
target sum to search for. - It is assumed that the List always has at least one node. - All the
elements in the List are assumed to be non-negative integers. - If no sequence of initial elements
adds up to n , the method will return a negative value, which is specified as follows: 1. The
negative of the size of the List if the sum of all elements in the List is less than n . 2. The
negative of the minimum number of elements at the beginning of the List whose sum exceeds n .
Examples (these show the lists as arrays, but you will only be given the head node of each singly
linked list) 1. A call to hasprefixsum ({ 1 , 2 , 3 , 4 , 5 } , 10 ) returns 4 since 1 + 2 + 3 + 4 = 10 ,
which is an exact sum of 10 with the first 4 elements of the list. 2. A call to hasprefixsum ({ 2 , 4
, 6 , 8 , 10 } , 10 ) returns 3 since 2 + 4 + 6 = 12 , which is larger than 10. 3. A call to
hasprefixsum ({ 1 , 2 , 3 , 4 } , 15 ) returns 4 since 1 + 2 + 3 + 4 = 10 , which is smaller than 15
and the list does not have enough elements to sum up to 15