Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 { private int size; private Node head; private Node tail; private class Node{ E data; Node link; // or 'next' Node(E newData){ data = newData; link = null; } } MyLinkedList(){ size = 0; head = null; tail = null; }

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{ Node nextNode; // to point [next node] Node object Node lastReturned; // to point [last returned] Node object Node prevNode; // to point the [previous node of the last returned] Node object (to remove lastReturned) Iterator(){ // In this constructor, you must set nextNode, lastReturned, prevNode variables // next must be the first node of the list // lastReturned and prevNode must be null when the Iterator is created // Write code here } public E next() throws RuntimeException{ // return the data_field of [next node] // caution: update nextNode, lastReturned, prevNode correctly // Throw an Exception if the iteration has no more elements // Write code here } public boolean hasNext(){ // return true when the [next node] exists // return false when we don't have the [next node] // Write code here } public void remove() { // Using prevNode and lastReturned, remove [last returned node] which is already read by next() method // If nothing read(by next() operation) yet, throw an Exception such as... throw new RuntimeException("Can't operate remove"); // After remove(), you need to update lastReturned to prevent another remove() operation. // Write code here } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

Students also viewed these Databases questions