Q Write Method Return Circularlylinkedlist Reversed String Example Elements List 1 2 3 4 5 Q34949983

Q:

Write a method that return CircularlyLinkedList as reversedstring

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 howmany 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 linkedlist to the new linked list in reverse order (you should use one ofthe two method addFirst() or addLast() to add in reverse

2- you should traverse the new list and add the element insideeach node to string don’t forget the space in the string. and thereis no comma after last element

Code to use:

class Main {
public static void main(String[] args) {
//test your implmentation
CircularlyLinkedList<Integer> l=newCircularlyLinkedList();
l.addLast(1);
l.addLast(2);
l.addLast(3);
l.addLast(4);
l.addLast(5);
l.addLast(6);
System.out.print(l.reverse());
  
}
}
class CircularlyLinkedList<E> {
//—————- nested Node class —————-
private static class Node<E> {

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

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

// Modifier methods
public void setNext(Node<E> n) { next = n; }
} //———– end of nested Node class ———–

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

/** Constructs an initially empty list. */
public CircularlyLinkedList() { } // constructs an initially emptylist

// access methods
public int size() { return size; }
public boolean isEmpty() { return size == 0; }
public E first() { // returns (but does not remove) the firstelement
if (isEmpty()) return null;
return tail.getNext().getElement(); // the head is *after* thetail
}
public E last() { // returns (but does not remove) the lastelement
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 ofthe list
// the old head becomes the new tail
}

public void addFirst(E e) { // adds element e to the front ofthe list
if (size == 0) {
tail = new Node<>(e, null);
tail.setNext(tail); // link to itself circularly
} else {
Node<E> newest = new Node<>(e, tail.getNext());
tail.setNext(newest);
}
size++;
}
public void addLast(E e) { // adds element e to the end of thelist
addFirst(e); // insert new element at front of list
tail = tail.getNext(); // now new element becomes the tail
}
  
public Node<E> 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 itin revrse order
//you should use one of the two method addFirst() or addLast() toadd in reverse
while(c!=null){
newList.addLast(c.getElement());
c.getNext();
}

  
//loop the new linked list to store the element on s
//hint :instead of prinitng add the new element to the string i.es=(previous content of s) +r.getElement() + ,
//note that there are no comma after last elment.
Node r =newList.getTail().getNext();
while(r!=null){
s=(s)+r.getElement();
System.out.print(s);
s=””;
if(r!=tail)
System.out.print(r+”, “);

r.getNext();
}
  
return s;
}
}

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *