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

Hello I hope to solve this question in java language this is the help files to a

ID: 3685858 • Letter: H

Question

Hello

I hope to solve this question in java language

this is the help files to answer it

www.moussaacademy.com/java.zip

regards

To solve this question, you can use the doubly LinkedList Class from your book, or any other implementation. Add the following changes to the doubly LinkedList Class: Override the toString() method. Implement getSize() method, which returns the current size of the linked list (remember that you need to make core changes to the code) Implement another toString method with the name reverseToString(), this method should return a string of the data within the linked list but in reverse order. Implement a sort() method to sort LinkedList. You can use any sort algorithm or any trick to sort LinkedList (think outside the box). The attached LinkedListTester class must work with your implementation.

Explanation / Answer

Hi, Chnags made in LinkedList.hava as per your requirement.

LinkedList.java


import java.util.NoSuchElementException;

/**
An implementation of a doubly linked list.
*/
public class LinkedList
{
private Node first;
private Node last;

/**
Constructs an empty linked list.
*/
public LinkedList()
{
first = null;
last = null;
}

/**
Returns the first element in the linked list.
@return the first element in the linked list
*/
public Object getFirst()
{
if (first == null) { throw new NoSuchElementException(); }
return first.data;
}

/**
Removes the first element in the linked list.
@return the removed element
*/
public Object removeFirst()
{
if (first == null) { throw new NoSuchElementException(); }
Object element = first.data;
first = first.next;
if (first == null) { last = null; } // List is now empty
else { first.previous = null; }
return element;
}

/**
Adds an element to the front of the linked list.
@param element the element to add
*/
public void addFirst(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = first;
newNode.previous = null;
if (first == null) { last = newNode; }
else { first.previous = newNode; }
first = newNode;
}

/**
Returns the last element in the linked list.
@return the last element in the linked list
*/
public Object getLast()
{
if (last == null) { throw new NoSuchElementException(); }
return last.data;
}

/**
Removes the last element in the linked list.
@return the removed element
*/
public Object removeLast()
{
if (last == null) { throw new NoSuchElementException(); }
Object element = last.data;
last = last.previous;
if (last == null) { first = null; } // List is now empty
else { last.next = null; }
return element;
}

/**
Adds an element to the back of the linked list.
@param element the element to add
*/
public void addLast(Object element)
{
Node newNode = new Node();
newNode.data = element;
newNode.next = null;
newNode.previous = last;
if (last == null) { first = newNode; }
else { last.next = newNode; }
last = newNode;
}

/**
Returns an iterator for iterating through this list.
@return an iterator for iterating through this list
*/
public ListIterator listIterator()
{
return new LinkedListIterator();
}

class Node
{
public Object data;
public Node next;
public Node previous;
}

class LinkedListIterator implements ListIterator
{
private Node position;
private boolean isAfterNext;
private boolean isAfterPrevious;

/**
Constructs an iterator that points to the front
of the linked list.
*/
public LinkedListIterator()
{
position = null;
isAfterNext = false;
isAfterPrevious = false;
}
  
/**
Moves the iterator past the next element.
@return the traversed element
*/
public Object next()
{
if (!hasNext()) { throw new NoSuchElementException(); }
isAfterNext = true;
isAfterPrevious = false;

if (position == null)
{
position = first;
}
else
{
position = position.next;
}

return position.data;
}
  
/**
Tests if there is an element after the iterator position.
@return true if there is an element after the iterator position
*/
public boolean hasNext()
{
if (position == null)
{
return first != null;
}
else
{
return position.next != null;
}
}

/**
Moves the iterator before the previous element.
@return the traversed element
*/
public Object previous()
{
if (!hasPrevious()) { throw new NoSuchElementException(); }
isAfterNext = false;
isAfterPrevious = true;

Object result = position.data;
position = position.previous;
return result;
}
  
/**
Tests if there is an element before the iterator position.
@return true if there is an element before the iterator position
*/
public boolean hasPrevious()
{
return position != null;
}
  
/**
Adds an element before the iterator position
and moves the iterator past the inserted element.
@param element the element to add
*/
public void add(Object element)
{
if (position == null)
{
addFirst(element);
position = first;
}
else if (position == last)
{
addLast(element);
position = last;
}
else
{
Node newNode = new Node();
newNode.data = element;
newNode.next = position.next;
newNode.next.previous = newNode;
position.next = newNode;
newNode.previous = position;
position = newNode;
}

isAfterNext = false;
isAfterPrevious = false;
}
  
/**
Removes the last traversed element. This method may
only be called after a call to the next() method.
*/
public void remove()
{
Node positionToRemove = lastPosition();

if (positionToRemove == first)
{
removeFirst();
}
else if (positionToRemove == last)
{
removeLast();
}
else
{
positionToRemove.previous.next = positionToRemove.next;
positionToRemove.next.previous = positionToRemove.previous;
}

if (isAfterNext)
{
position = position.previous;
}

isAfterNext = false;
isAfterPrevious = false;
}

/**
Sets the last traversed element to a different value.
@param element the element to set
*/
public void set(Object element)
{
Node positionToSet = lastPosition();
positionToSet.data = element;
}

/**
Returns the last node traversed by this iterator, or
throws an IllegalStateException if there wasn't an immediately
preceding call to next or previous.
@return the last traversed node
*/
private Node lastPosition()
{
if (isAfterNext)
{
return position;
}
else if (isAfterPrevious)
{
if (position == null)
{
return first;
}
else
{
return position.next;
}
}
else { throw new IllegalStateException(); }
}
}

public String toString(){

//your code goes here
   String str = "";
   if (first == null) {
       System.out.println("List is null.");
       str = "List is null.";
       return str;
       }
      
       for (Node current = first; current != null; current = current.next){
       str = str + current.data + " ";
       }
return str;

}

public String backwardToString(){

//your code goes here
   String str = "";
Node curNode = this.last;
while(curNode != null) {
str = str + curNode.data + " ";
curNode = curNode.previous;
}
return str;

}

public int getSize(){

//your code goes here
int count = 0;
for (Node current = first; current != null; current = current.next)
count++;
return count;

}

public void sort(){
   Node current = first;
   Node tail = null;
   while(current != null&& tail != first )
   {
       Node next = current;
   for( ; next.next != tail; next = next.next)
   {
   if(Integer.parseInt(""+next.data) >= Integer.parseInt(""+next.next.data))
   {
   int temp = Integer.parseInt(""+next.data);
   next.data = next.next.data;
   next.next.data = temp;
   }
   }
   tail = next;
   current = first;
   }
}
}

Output:

myLis size = 10.

myList before sorting:
64 70 15 64 21 87 21 84 91 92

After sorting(ascending):
15 21 21 64 64 70 84 87 91 92

After sorting(descending):
92 91 87 84 70 64 64 21 21 15

myLis size = 10.

myList before sorting:
78 52 66 51 67 91 91 12 23 19

After sorting(ascending):
12 19 23 51 52 66 67 78 91 91

After sorting(descending):
91 91 78 67 66 52 51 23 19 12

Note: No changes in other java files.

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