Question: solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For example: If the elements of a list is 1,

solve this Q in java languege

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

implement reverse method

you have two steps:

1- you should start traversing from the last element of DoublyLinkedList (the previous of the trailer)

2- you should add the element inside each node to string don't forget the space in the string. and there is no comma after last element

the is codee:

class Main { public static void main(String[] args) { //test you implmentation DoublyLinkedList l=new DoublyLinkedList(); l.addFirst(6); l.addFirst(5); l.addFirst(4); l.addFirst(3); l.addFirst(2); l.addFirst(1); System.out.print(l.reverse());

} }

class DoublyLinkedList {

//---------------- nested Node class ---------------- private static class Node {

private E element; // reference to the element stored at this node private Node prev; // reference to the previous node in the list private Node next; // reference to the subsequent node in the list

public Node(E e, Node p, Node n) { element = e; prev = p; next = n; }

// public accessor methods public E getElement() { return element; } public Node getPrev() { return prev; } public Node getNext() { return next; }

// Update methods public void setPrev(Node p) { prev = p; } public void setNext(Node n) { next = n; } } //----------- end of nested Node class -----------

// instance variables of the DoublyLinkedList private Node header; // header sentinel private Node trailer; // trailer sentinel private int size = 0; // number of elements in the list

public DoublyLinkedList() { header = new Node<>(null, null, null); // create header trailer = new Node<>(null, header, null); // trailer is preceded by header header.setNext(trailer); // header is followed by trailer }

// public accessor methods

public int size() { return size; } public boolean isEmpty() { return size == 0; }

public E first() { if (isEmpty()) return null; return header.getNext().getElement(); // first element is beyond header }

public E last() { if (isEmpty()) return null; return trailer.getPrev().getElement(); // last element is before trailer }

// public update methods public void addFirst(E e) { addBetween(e, header, header.getNext()); // place just after the header }

public void addLast(E e) { addBetween(e, trailer.getPrev(), trailer); // place just before the trailer }

// private update methods private void addBetween(E e, Node predecessor, Node successor) { // create and link a new node Node newest = new Node<>(e, predecessor, successor); predecessor.setNext(newest); successor.setPrev(newest); size++; }

public String reverse () { String s=""; Node c= trailer.prev; DoublyLinkedList D1= new DoublyLinkedList(); //taraverse the list starting from the last element to the first //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. } return s; } } //----------- end of DoublyLinkedList class -----------

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!