Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Return a string for the content of SingleyLinkedList in reverse order by using recursive algorithm. Signature: String reverse (Node 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 l=new SinglyLinkedList(); l.addFirst("aaa"); l.addFirst("bbbb"); l.addFirst("ccc"); System.out.print(l.reverse()); } }

class SinglyLinkedList { private static class Node { private E element; // reference to the element stored at this node private Node next; // reference 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; } }

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( ) { } public int size( ) { return size; } public boolean isEmpty( ) { return size == 0; } public Node 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 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; } }

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

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions