Question
Java Class file MyLinkedList.java is provided. Implement all empty methods. (Including iterators methods) - getFirst(), getLast(), addLast(), removeLast(), indexOf() You need to implement these -
Java Class file MyLinkedList.java is provided. Implement all empty methods. (Including iterators methods) - getFirst(), getLast(), addLast(), removeLast(), indexOf() You need to implement these - next(), hasNext(), remove() of Iterator You need to implement these - toString(), add(), addFirst(), remove(), removeFirst() these are already implemented
public class MyLinkedList
public E getFirst() throws RuntimeException{ // return the first element // if the list is empty, throw RuntimeException with a message // Write code here } public E getLast() throws RuntimeException{ // return the last element // if the list is empty, throw RuntimeException with a message // Write code here } public void addLast(E newElement){ // add a new Node to be the last element. // Write code here } public void removeLast(){ // Case 1: if the list is empty --> do nothing // Case 2: if you have only one element --> remove it // Case 3: in the list has two or more elements(general case) // Caution: you must care about the [tail] after removal // Write code here } public int indexOf(E targetElement){ // Returns the index of the first occurrence of the specified element(targetElement) in this list, // or -1 if this list does not contain the element. // Caution: index starts with 0 (the first element's index is 0) // Caution: to return index, you must check the index of node while you searching // Caution: When you compare two elements, please use 'equals' method instead of == // Write code here } public int size() { return size; } public boolean isEmpty() { return size==0; } public void addFirst(E newElement){ Node newNode = new Node(newElement); newNode.link = head; head = newNode; if(newNode.link == null) { tail = newNode; } size++; } public void add(int index, E newElement) throws RuntimeException { if(index<0 || index>size) throw new RuntimeException("In add method: Index out of bounds"); if (index == 0) { addFirst(newElement); } else { Node temp = head; while (--index > 0) { temp = temp.link; } Node newNode = new Node(newElement); newNode.link = temp.link; temp.link = newNode; if (newNode.link == null) { tail = newNode; } size++; } } public void removeFirst() throws RuntimeException{ if(head == null) { throw new RuntimeException("In removeFirst: the list is empty"); } head = head.link; if (head == null) { tail = null; } size--; } public void remove(int index) throws RuntimeException{ if(index<0 || index>=size) throw new RuntimeException("In remove method: Index out of bounds"); if (index == 0) { removeFirst(); } else { Node temp = head; while (--index > 0) { temp = temp.link; } Node targetNode = temp.link; temp.link = targetNode.link; size--; if(temp.link == null) { tail = temp; } } } public String toString() { String str = "["; Node temp = head; while(temp != null) { str = str + temp.data; if(temp != tail) { str = str + ", "; } temp = temp.link; } return str + "]"; } public Iterator iterator(){ // Write code here // create a new Iterator object and return it. return new Iterator(); } // Complete the Iterator class class Iterator implements java.util.Iterator
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started