Question
You are asked to add a method reverse to SinglyLinkedList class by using only a constant amount of additional space. Write the main method to
You are asked to add a method reverse to SinglyLinkedList class by using only a constant amount of additional space. Write the main method to test the reverse method. (JAVA)
package exercise1;
import javafx.scene.Node;
public class SinglyLinkedList
//---------nested Node class -----------
private static class Node
private E element; //reference to the element stored at this node
private Node
public Node(E e, Node
element = e;
next = n;
}
public E getElement(){
return element;
}
public Node
return next;
}
public void setNext(Node
next = n;
}
}
//---------end of nested Node class -----------
//instance variables of the SinglyLinkedList
private Node
private Node
private int size = 0; //number of nodes in the list
public SinglyLinkedList(){ //constructs an initially empty list
//access method
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public E first(){ //returns (but does not remove) the first element
if (isEmpty()) return null;
return head.getElement();
}
public E last(){ //returns (but does not remove) the last element
if (isEmpty()) return null;
return tail.getElement();
}
public void addFirst(E e){
head = new Node<>(e, head);
if(size == 0)
tail = head;
size++;
}
public void addLast(E e){
Node
if (isEmpty())
head = newest;
else
tail.setNext(newest);
tail = newest;
size ++;
}
}
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