Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 next; // ref to the subsequent node in the list

public Node(E e, Node n){

element = e;

next = n;

}

public E getElement(){

return element;

}

public Node getNext(){

return next;

}

public void setNext(Node n){

next = n;

}

}

//---------end of nested Node class -----------

//instance variables of the SinglyLinkedList

private Node head = null; //head node of the list (or null if empty)

private Node tail = null; //last node of the list (or null if empty)

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 newest = new Node<>(e, null);

if (isEmpty())

head = newest;

else

tail.setNext(newest);

tail = newest;

size ++;

}

}

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

More Books

Students also viewed these Databases questions

Question

Identify five strategies to prevent workplace bullying.

Answered: 1 week ago