Question

solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For...

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<Integer> l=new DoublyLinkedList<Integer>();
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<E> {

//---------------- nested Node class ----------------
private static class Node<E> {


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

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

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

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

// instance variables of the DoublyLinkedList
private Node<E> header; // header sentinel
private Node<E> 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<E> predecessor, Node<E> successor) {
// create and link a new node
Node<E> 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 -----------

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

DoublyLinkedList.java


public class DoublyLinkedList <E>
{
//---------------- nested Node class ----------------
private static class Node<E>
{
private E element; // reference to the element stored at this node
private Node<E> prev; // reference to the previous node in the list
private Node<E> next; // reference to the subsequent node in the list

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

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

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

// instance variables of the DoublyLinkedList
private Node<E> header; // header sentinel
private Node<E> 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<E> predecessor, Node<E> successor) {
// create and link a new node
Node<E> 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();
while(c.prev.getPrev()!=null)
{
s+=c.getElement()+", ";
c=c.prev;
}
s+=c.getElement();
//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() + ,
return s;
} //note that there are no comma after last elment.
}


  


  
  
//----------- end of DoublyLinkedList class -----------

Main.java

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

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
solve this Q in java languege Write a method that return DoublyLinkedList as reversed string For...
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
  • In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying...

    In Java Language. Modify the LinkedPostionalList class to support a method swap(p,q) that causes the underlying nodes referenced by positions p and q to be exchanged for each other. Relink the existing nodes, do not create any new nodes. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- package lists; import java.util.Iterator; import java.util.NoSuchElementException; public class LinkedPositionalList implements PositionalList { //---------------- nested Node class ---------------- /** * Node of a doubly linked list, which stores a reference to its * element and to both the previous and next...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Please answer in Java. The code that needs to be modified is below. Thank you. Question:...

    Please answer in Java. The code that needs to be modified is below. Thank you. Question: Implement Doubly Linked List add method which add an element to a specific position. - It’s an instance method that takes a position and an element, then adds the element to this specific position and shifts the element currently at this position and any subsequent elements to the right. It throws an exception if the position is out of bound. It traverses the list...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

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
ADVERTISEMENT