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 l=new CircularlyLinkedList(); } } class CircularlyLinkedList { //---------------- nested Node class ---------------- private static class Node {

private E element; // an element stored at this node private Node next; // a reference to the subsequent node in the list public Node(E e, Node n) { element = e; next = n; }

// Accessor methods public E getElement() { return element; } public Node getNext() { return next; }

// Modifier methods public void setNext(Node n) { next = n; } } //----------- end of nested Node class -----------

// instance variables of the CircularlyLinkedList private Node tail = null; // we store tail (but not head) private int size = 0; // number of nodes in the list

/** 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 newest = new Node<>(e, tail.getNext()); tail.setNext(newest); } size++; } public void addLast(E e) { // adds element e to the end of the list addFirst(e); // insert new element at front of list tail = tail.getNext(); // now new element becomes the tail } public Node getTail() { return tail; } public String reverse() { String s=""; CircularlyLinkedList newList= new CircularlyLinkedList(); Node c =tail.next; //define a new list and add the element of the current list to it in revrse order //you should use one of the two method addFirst() or addLast() to add in reverse //loop the new linked list to store the element on s //hint :instead of prinitng add the new element to the string i.e s=(previous content of s) +r.getElement() + , //note that there are no comma after last elment. Node r =newList.getTail().getNext(); return s; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!