Question: Solve this question with (java) language with this code this is QUESTION : Write a method that return CircularlyLinkedList as reversed string For example: If
Solve this question with (java) language with this code
this is QUESTION : Write a method that return CircularlyLinkedList as reversed string
For example:
If the elements of a list is
1, 2, 3, 4, 5, 6
the reverse string should be
6, 5, 4, 3, 2, 1
you should write rotate method your self, do you remember how many line in the algorithm? re write that line.
Implement reverse method
you have two steps:
1- creating a new linked list and all node of the first linked list to the new linked list in reverse order (you should use one of the two method addFirst() or addLast() to add in reverse
2- you should traverse the new list and add the element inside each node to string don't forget the space in the string. and there is no comma after last element
class Main {
public static void main(String[] args) { //test your implmentation CircularlyLinkedList
private E element; // an element stored at this node private Node
// Accessor methods public E getElement() { return element; } public Node
// Modifier methods public void setNext(Node
// instance variables of the CircularlyLinkedList private Node
/** Constructs an initially empty list. */ public CircularlyLinkedList() { } // constructs an initially empty list
// access methods 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 tail.getNext().getElement(); // the head is *after* the tail } public E last() { // returns (but does not remove) the last element if (isEmpty()) return null; return tail.getElement(); }
// update methods /** * Rotate the first element to the back of the list. */ public void rotate() { // rotate the first element to the back of the list // the old head becomes the new tail }
public void addFirst(E e) { // adds element e to the front of the list if (size == 0) { tail = new Node<>(e, null); tail.setNext(tail); // link to itself circularly } else { Node
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
