Question

Return a string for the content of SingleyLinkedList in reverse order by using recursive algorithm. Signature:...

Return a string for the content of SingleyLinkedList in reverse order by
using recursive algorithm.
Signature: String reverse (Node<E> yourVariableName)
Note: the class name should be SingleyLinkedList

just Convert the reverse method to recursive algorithm.

this is class SingleyLinkedList with reverse method:

class Main {
public static void main(String[] args) {
  
SinglyLinkedList<String> l=new SinglyLinkedList<String>();
l.addFirst("aaa");
l.addFirst("bbbb");
l.addFirst("ccc");
  
  
System.out.print(l.reverse());
}
}

class SinglyLinkedList<E>
{
  
private static class Node<E>
{
    private E element; // reference to the element stored at this node
    private Node<E> next; // reference to the subsequent node in the list
    public Node(E e, Node<E> n)
    {
    element = e;
   next = n;
   }
public E getElement( ) { return element; }
    public Node<E> getNext( ) { return next; }
    public void setNext(Node<E> n) { next = n; }
    }

private Node<E> head = null; // head node of the list (or null if empty)
    private Node<E> tail = null; // last node of the list (or null if empty)
    private int size = 0; // number of nodes in the list
      
    public SinglyLinkedList( ) { }
    public int size( ) { return size; }
    public boolean isEmpty( ) { return size == 0; }
      
    public Node<E> getHead( )
    { // returns the head node
   if (isEmpty( )) return null;
    return head;
    }

      
    public void addFirst(E e)
    { // adds element e to the front of the list
head = new Node<>(e, head); // create and link a new node
    if (size == 0)
    tail = head; // special case: new node becomes tail also
   size++;
}

public void addLast(E e) { // adds element e to the end of the list
Node<E> newest = new Node<>(e, null); // node will eventually be the tail
if (isEmpty())
head = newest; // special case: previously empty list
else
tail.setNext(newest); // new node after existing tail
tail = newest; // new node becomes the tail
size++;
}

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( );
}
      
    //implment this Method
public String reverse()
{
String s="";
SinglyLinkedList l2= new SinglyLinkedList();
Node c =head;
//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
while (c!=null){
l2.addFirst(c.element);
c=c.getNext();
}

//note that there are no comma after last elment.
Node r =l2.getHead();
while (r!=null){
s=s+Integer.toString((Integer)r.getElement()); //loop the new linked list to store the element on s
if(r.getNext()!=null){
s=s+", "; }
r=r.getNext();}
return s;

}
  
}
  
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//recursive method
//to return the contents of the list in reverse order
String reverse(Node<E> head) {
if(head == null) {
return "";
}
  
// last node or only one node
if(head.next == null) {
return Integer.toString((Integer)head.getElement());//returning element
}
  
       String s = "";
         
s = reverse(head.getNext()); //recursive call
  
    s= Integer.toString((Integer)head.getElement())+","+s;
    return s;
}

Add a comment
Know the answer?
Add Answer to:
Return a string for the content of SingleyLinkedList in reverse order by using recursive algorithm. Signature:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT