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...

  • -Fill in the reverse method below to return a new DoublyLinkedList consisting of the same elements...

    -Fill in the reverse method below to return a new DoublyLinkedList consisting of the same elements in reverse order. -The reverse method must not modify the original DoublyLinkedList. -The reverse method must run in linear time. Can someone answer this for me, please? In Java public class DoublyLinkedList { int size; Node firstNode; Node lastNode; public DoublyLinkedList() { size = 0; firstNode = null; lastNode = null; } // Problem 1 (15 pts) // Fill in the method below to...

  • Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of...

    Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...

  • 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...

  • Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of...

    Modify Dlinkedlist.java so it includes a new method named getPriorNode that accepts the String value of a potential element in the doubly linked list and returns the string value of the predecessor node (or an appriopriate message String if there is no such node or predecessor. class DLinkedList { /** The Node class stores a list element and a reference to the next node. */ private class Node { String value; // Value of a list element Node next; //...

  • 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...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • 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...

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