Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is QUESTION : 1 Write a method that return SinglyLinkedList as reversed string For example: If the elements of a list is 1, 2,

this is QUESTION :

1 Write a method that return SinglyLinkedList 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

it should be printed in main

hint: a new list maybe a solution. use addFirst() or addLast()

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 here SinglyLinkedList l=new SinglyLinkedList(); l.addFirst(6); l.addFirst(5); l.addFirst(4); l.addFirst(3); l.addFirst(2); l.addFirst(1); 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 } //note that there are no comma after last elment. //loop the new linked list to store the element on s } 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

Generative Artificial Intelligence For Project Management With Aws

Authors: Timothy Krimmel

1st Edition

B0CQV9KWB8, 979-8872627197

More Books

Students also viewed these Databases questions