Write this code in JAVA The MyLinkedList class used in Listing 24.6 is a one-way
ID: 3603771 • Letter: W
Question
Write this code in JAVA
The MyLinkedList class used in Listing 24.6 is a one-way directional lnked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows: public class NodecE> E element: Node next; NodecE> previous: public Node CE ) elemente Implement a new class named TwoWayLinkedList that uses a doubly linked list to store cle- ments. The MyLinkedList class in the text extends MyAbstractList. Define TwoWayLinkedList to extend the java.util.AbstractSequentialList class. You nced to implement all the methods defined in MyLinkedList as well as the methods listIterator) and listIterator(int index). Both return an instance of java.util.ListIterator. The former sets the cursor to the hcad of the list and the latter to the element at the specified indexExplanation / Answer
Program:
public class TwoWayLinkedList<E> extends AbstractSequentialList<E>{
private Node head;
private Node tail;
private int size;
public TwoWayLinkedList() {
size = 0;
}
/**
* Returns the size of the linked list
*/
@Override
public int size() {
return this.size;
}
/**
* Returns if the list is empty or not
*/
public boolean isEmpty() { return size == 0; }
/**
* Adds element at the starting of the linked list
*/
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if(head != null ) {head.previous = tmp;}
head = tmp;
if(tail == null) { tail = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* Adds element at the end of the linked list
*/
public void addLast(E element) {
Node tmp = new Node(element, null, tail);
if(tail != null) {tail.next = tmp;}
tail = tmp;
if(head == null) { head = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* Removes element from the start of the linked list
*/
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
Node<E> tmp = head;
head = head.next;
head.previous = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
/**
* This method removes element from the end of the linked list
*/
public E removeLast() {
if (size == 0) throw new NoSuchElementException();
Node<E> tmp = tail;
tail = tail.previous;
tail.next = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
public ListIterator<E> listIterator() {
return listIterator(0);
}
public ListIterator<E> listIterator(int index) {
return new TwoWayLinkedListIterator<E>(index);
}
private class TwoWayLinkedListIterator<E> implements ListIterator<E> {
private Node<E> current=head;
/**
* Builds a new iterator at the given index.
*/
private TwoWayLinkedListIterator(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("index: " + index +
", size: " + size());
}else {
while (size < index) {
this.next();
}
}
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public E next() {
if (!hasNext()) throw new NoSuchElementException();
E tmp = current.element;
current = current.next; // if next is null, hasNext will return false.
return tmp;
}
@Override
public boolean hasPrevious() {
// TODO Auto-generated method stub
return false;
}
@Override
public E previous() {
// TODO Auto-generated method stub
return null;
}
@Override
public int nextIndex() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int previousIndex() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
@Override
public void set(E e) {
// TODO Auto-generated method stub
}
@Override
public void add(E e) {
// TODO Auto-generated method stub
}
}
}
Main method to test:
public static void main(String args[]){
TwoWayLinkedList<Integer> twl = new TwoWayLinkedList<Integer>();
twl.addFirst(11);
twl.addFirst(36);
twl.addLast(86);
twl.addLast(398);
twl.listIterator();
twl.listIterator(2);
twl.removeFirst();
twl.removeLast();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.