Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a new class named MyTwoWayLinkedList that uses a doubly linked list to

ID: 667910 • Letter: I

Question

Implement a new class named MyTwoWayLinkedList that uses a doubly linked list to store elements. The MyLinkedList to extend the java.util.AbstractSequentialList class. You need to implement the methods listIterator() and listIterator(int index). Both return an instance of java.util.ListIterator<E>. The former sets the cursor to the head of the list and the latter to the elementat the specified index.

public class MyLinkList<E> extends MyAbstractList<E> {
private Node<E> head, tail;
  
MyLinkList(){}
  
MyLinkList(E[] object){
super(object);
}

@Override
public void add(int index, E e) {
if(index == 0){
addFirst(e);
}
else if(index >= size){
addLast(e);
}
else{
Node<E> current = head;
for(int i = 1; i < index; i++){
current = current.next;
}
Node<E> temp = current.next;
  
current.next = new Node<E>(e);
temp.previous = current.next;
(current.next).next = temp;
size++;
}
}
  
public void addFirst(E e){
Node<E> newNode = new Node<E>(e);
if(size == 0){
newNode.next = head;
head = newNode;
size++;
}
else{
head.previous = newNode;
newNode.next = head;
head = newNode;
size++;
}
  
if(tail == null){
tail = head;
}
}
  
public void addLast(E e){
Node<E> newNode = new Node<E>(e);
if(tail == null){
head = tail = newNode;
}
else{
newNode.previous = tail;
tail.next = newNode;
tail = tail.next;
}
  
size++;
}

@Override
public void clear() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public boolean contains(E e) {
Node<E> current = head;
  
for(int i = 0; i < size; i++){
if((current.element.equals(e))){
return true;
}
  
current = current.next;
}
  
return false;
}

@Override
public E get(int index) {
if (index < 0 || index > size - 1)
return null;
  
if(index == 0){
return getFirst();
}
else if(index == size()){
return getLast();
}
else{
Node<E> current = head;
for(int i = 1; i < index; i++){
current = current.next;
}
E temp = (current.next).element;
  
return temp;
}
}
  
public E getFirst(){
if(size == 0){
return null;
}
return head.element;
}
  
public E getLast(){
if(size == 0){
return null;
}
else{
return tail.element;
}
}

@Override
public int indexOf(E e) {
Node<E> current = head;
for(int i = 0; i < size; i++){
if(current.element.equals(e)){
return i;
}
current = current.next;
}
  
return -1;
}

@Override
public int lastIndexOf(E e) {
Node<E> current = head;
int index = 0;
boolean indexIsFounded = false;


for(int i = 0; i < size; i++){
if(current.element.equals(e)){
index = i;
indexIsFounded = true;
}
  
current = current.next;
}
  
if(indexIsFounded){
return index;
}
else{
return -1;
}
}

@Override
public E remove(int index) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public Object set(int index, E e) {
Object temp;
if(size == 0){
addFirst(e);
return null;
}
else if(index == 0){
temp = head.element;
head.element = e;
return temp;
}
else if(index == size - 1){
temp = tail.element;
tail.element = e;
return temp;
}
else{
Node<E> current = head;
for(int i = 0; i < index - 1; i++){
current = current.next;
}
temp = current.next.element;
current = current.next;
current.element = e;
}
  
return temp;
}
  
@Override
public Iterator<E> iterator() {
return new LinkedListIterator();
}
  
private class LinkedListIterator implements java.util.Iterator<E>{
private Node<E> current = head;
  
@Override
public boolean hasNext() {
return (current != null);
}

@Override
public E next() {
E e = current.element;
current = current.next;
return e;
}
}
  
private static class Node<E>{
E element;
Node<E> next;
Node<E> previous;
  
Node(E element){
this.element = element;
}
}
  
public class MyTwoWayLinkedList<E> extends java.util.AbstractSequentialList<E>{

MyTwoWayLinkedList(){}
  
@Override
public java.util.ListIterator listIterator(int index) {
MyIterator<E> myIter = new MyIterator<E>();
for(int i = 0; i < index; i++){
myIter.next();
}
return myIter;
}
  
public java.util.ListIterator listIterator() {
MyIterator<E> myIter = new MyIterator<E>();
myIter.current = (Node<E>) head;
return myIter;
}

@Override
public int size() {
return size;
}
  
  
public class MyIterator<E> implements java.util.ListIterator<E>{
Node<E> current = (Node<E>) head;

int index = 0;
  
@Override
public boolean hasNext() {
return (current != null);
}

@Override
public E next() {
E e = current.element;
current = current.next;
index++;
return e;
}

@Override
public boolean hasPrevious() {
return current.previous != null;
}

@Override
public E previous() {
E e = current.element;
current = current.previous;
index--;
return e;
}

@Override
public int nextIndex() {
if(index + 1 >= size){
throw new IndexOutOfBoundsException("index " + index + 1 + " out of bounds");
}
return index + 1;
}

@Override
public int previousIndex() {
if(index - 1 < 0){
throw new IndexOutOfBoundsException("index " + index + 1 + " out of bounds");
}
return index - 1;
}

@Override
public void remove() {
  
}

@Override
public void set(E e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void add(E e) {
Node newNode = new Node<E>(e);
  
if(head == null){
tail = head = newNode;
}
else{
tail.next = newNode;
newNode.previous = tail;
tail = tail.next;
}
size++;
}
}
}

main:

public static void main(String[] args) {

list1.add("tom");
list1.add("mary");
list1.add("vincent");

while(list1.listIterator().hasNext()){
System.out.println(list1.listIterator().next());
}

1. After I create a MyTwoWayLinkedList (which is the inner class object), when I use the while loop and call the hasNext() and next() methods to display the element in doubly linked list, it would become an infinity loop. Same thing happen on the MyLinkList class. But I believe I implement the method correctly.

2. The add(E e) method in MyIterator class, when I try to implement it by using the MyLinkList.this.add(size, e), it gives me an error.

3. Am I implement all the methods correctly regarding the question?

Explanation / Answer

1) You are not checking for next value is null or not...thats why it was going to infinity loop
public boolean hasNext()
{
if(current.next == null) //check whether next element is exist or not
{
return false;
}
return true;
}

-------------------------------------------------------------------------
2)
Dont call add(size,e) method directly....create MyLinkList object and call it with that object
Sice.. this methos is not prototype function we cannot call directly with class name.
creae object and call method on object.

---------------------------------------------------------------
3)
Yeah correctly implemented...small mistake regarding hasNext()...Try to use
netbeans or eclipse IDE which will help for auto generating code..and
checks for errors...I have small doubt...In your main method you are calling
add method on list object ..but you didn't created list obejct in main....check there also

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote