Question

To class DoublyLinkedList, add method reverse which reverses the order of the elements in the list....

To class DoublyLinkedList, add method reverse which reverses the order of the elements in the list.

For Java, JavaEclipse. Course Data Structures.

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

Sample Code of Reverse( ) :

public void reverse() {
// temp is helping variable for swaping
DLL_Node temp = null;
DLL_Node p_crawl = dll_head;
  
// swaping prev and next address of each node
while (p_crawl != null) {
temp = p_crawl.prev;
p_crawl.prev = p_crawl.next;
p_crawl.next = temp;
p_crawl = p_crawl.prev;
}
if (temp != null) {
dll_head = temp.prev;
}
}

Sample Code of running Full Java Program :

import java.lang.*;
import java.util.*;

public class DoublyLinkedList {
class DLL_Node{
int data;
DLL_Node prev; // store address of previous node
DLL_Node next; // store address of next node
  
// contrustor
DLL_Node(int data) {
this.data = data;
}
}
// delaration global variable
DLL_Node dll_head, dll_tail = null;
  
  
public void print_dll() {
DLL_Node p_crawl = dll_head;
if(p_crawl == null) {
return;
}
while(p_crawl != null) {
System.out.print(p_crawl.data + " ");
p_crawl = p_crawl.next;
}
System.out.println();
}
  
// inserting node at end of doublylinkedlist
public void appendDLL_Node(int data) {
DLL_Node dll_node = new DLL_Node(data);
// this condition is true when dll_head nas no node
if(dll_head == null) {
dll_head = dll_tail = dll_node;
dll_head.prev = null;
dll_tail.next = null;
}
else {
dll_tail.next = dll_node;
dll_node.prev = dll_tail;
dll_tail = dll_node;
dll_tail.next = null;
}
}
public void reverse() {
// temp is helping variable for swaping
DLL_Node temp = null;
DLL_Node p_crawl = dll_head;
  
// swaping prev and next address of each node
while (p_crawl != null) {
temp = p_crawl.prev;
p_crawl.prev = p_crawl.next;
p_crawl.next = temp;
p_crawl = p_crawl.prev;
}
if (temp != null) {
dll_head = temp.prev;
}
}
  
  
public static void main(String[] args) {
  
DoublyLinkedList dll = new DoublyLinkedList();
//Adding nodes for creating DoublyLinkedList
for(int i = 1;i<=10;i++)
dll.appendDLL_Node(i*10);
  
// calling function to print elements in doublylinkedlist
// before reverse
System.out.print("nodes before calling reverse function : ");
dll.print_dll();
// calling function to reverse
dll.reverse();
// calling function to print elements in doublylinkedlist
// after reverse
System.out.print("nodes after calling reverse function : ");
dll.print_dll();
  
}
}

Screenshot of Full Java Program :

Output Screenshot :

Please upvote if it helps. Feel free to comment if you have any query.

***thank you***

Add a comment
Know the answer?
Add Answer to:
To class DoublyLinkedList, add method reverse which reverses the order of the elements in the list....
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
  • -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...

  • complete this method: public void reverse() { } in ArrayList class implementing ListInterface, importing java.util.Arrays reverses...

    complete this method: public void reverse() { } in ArrayList class implementing ListInterface, importing java.util.Arrays reverses order of an array

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

  • Create a class called Reverse that reverses an array of integers. The class should have two...

    Create a class called Reverse that reverses an array of integers. The class should have two method: - public static void reverse(int [] array) – Which calls the private recursive method, passing the array parameter and which two array elements should be swapped. - private static void reverseRecursive(int [] array, int startIndex, int endIndex) – Which swaps the two elements of the array parameter pointed to by the startIndex and endIndex parameters. The method should be called again, this time,...

  • pls help, idk whats wrong with this Add the reverse() method which reverses the content of...

    pls help, idk whats wrong with this Add the reverse() method which reverses the content of array without using additional array and the rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete...

  • Using Java, Implement the size( ) method for the DoublyLinkedList class, assuming that we did not...

    Using Java, Implement the size( ) method for the DoublyLinkedList class, assuming that we did not keep the size variable as an instance variable.

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • in C++ function that reverses a linked list of integers and reverses the order of its...

    in C++ function that reverses a linked list of integers and reverses the order of its nodes. The function will have one call-by-reference parameter that is a pointer to the head of the list. After the function is called, this pointer will point to the head of a linked list that has the same nodes as the original list but in the reverse of the order they had in original list. Note that your function will neither create nor destroy...

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, ...

    STACK Data Structure using JAVA 1. In a FILO structure, elements are processed in reverse order, in which they arrive. Suppose during execution, an element of the stack becomes least important in terms of priority and has to be but at the bottom of the stack. Method Signature: public void decreasePriority(<AnyType> el) Write an operator that places a certain element of the stack at the bottom of the stack.

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