Question

In: Computer Science

Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the...

Method: DoublyLinkedList reverse(DoublyLinkedList list) Reverse() method accepts a DoublyLinkedList of Character as the argument, reverses the elements in the list, and returns the resulting list. For example:

The given list is

'a' 'b' 'c' 'd' 'e'

The return list should be

'e' 'd' 'c' 'b' 'a'

How can we do this in Java?

Solutions

Expert Solution

class LinkedList {

static Node head;

static class Node {

char data;
Node next, prev;

Node(char d) {
data = d;
next = prev = null;
}
}

/* Function to reverse a Doubly Linked List */
void reverse() {
Node temp = null;
Node current = head;

/* swap next and prev for all nodes of
doubly linked list */
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}

/* Before changing head, check for the cases like empty
list and list with only one node */
if (temp != null) {
head = temp.prev;
}
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(char new_data) {
/* allocate node */
Node new_node = new Node(new_data);

/* since we are adding at the beginning,
prev is always NULL */
new_node.prev = null;

/* link the old list off the new node */
new_node.next = head;

/* change prev of head node to new node */
if (head != null) {
head.prev = new_node;
}

/* move the head to point to the new node */
head = new_node;
}

/* Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked list */
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}

public static void main(String[] args) {
LinkedList list = new LinkedList();

/* Let us create a sorted linked list to test the functions
Created linked list will be 10->8->4->2 */
list.push('e');
list.push('d');
list.push('c');
list.push('b');
list.push('a');

System.out.println("Original linked list ");
list.printList(head);

list.reverse();
System.out.println("");
System.out.println("The reversed Linked List is ");
list.printList(head);
}
}

Output Screenshot:


Related Solutions

Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2)...
Add to the class DoublyLinkedList a method called isSubSet ( DoublyLinkedList <Character> s1, DoublyLinkedList <Character> s2) that receives 2 DoublLinked lists of characters. The method will return true if s1 is subset of s2. Then don’t forget to test the method in the main method in Test class in Doubly package.
Write a function void reverse(char * s) that reverses the string passed as an argument. Your...
Write a function void reverse(char * s) that reverses the string passed as an argument. Your code should use pointer arithmetic (it may increment and decrement pointers, but it may not use array indexing). Here is a piece of code that shows the behavior of reverse: char buf[100]; strcpy(buf, “hello”); reverse(buf); printf(“%s\n”, buf); // output should be olleh
implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't...
implement the reverse() method that changes the ordering of the items within a doublylinkedlist class. don't return anything and make sure it executes without errors on lists with no items or one item example: fruits = DoublyLinkedList() fruits.append('apple') fruits.append('banana') fruits.append('cherry') for i in fruits: print(i) >> apple >> banana >> Charlie fruits.reverse() for i in fruits: print(i) >> cherry >> banana >> apple
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack...
Write a method public static Stack reverse(Stack s) that reverses the order of elements on stack s using a Queue. Test your method using some example stacks. In java
Write a method that accepts a String object as an argument and displays its contents backward....
Write a method that accepts a String object as an argument and displays its contents backward. For instance, if the string argument is "gravity" the method should display "ytivarg". Demonstrate the method in a program that asks the user to input a string and then prints out the result of passing the string into the method. Sample Run java BackwardString Enter·a·string:Hello·world↵ dlrow·olleH↵
Task #1 Develop a recursive method to reverse a list Develop a method with the prototype...
Task #1 Develop a recursive method to reverse a list Develop a method with the prototype public static void reverse (ArrayList inputList) based on selecting the first list element as the head and the remaining list as its tail. Here is the recursive design. 1) Base case: The problem is trivial when the list size is 0 or 1. 2) Decomposition: For lists with size > 1: a) Extract its head (element) and leave the tail (the input list with...
Write, specify and prove the function reverse that reverses an array in place. Take care of...
Write, specify and prove the function reverse that reverses an array in place. Take care of the unmodified part of the array at some iteration of the loop. Assume that the swap function is already proved. Note: Prototype is as below. [7 M] [CO2] void swap(int* a, int* b); void reverse(int* array, size_t len){ }
Java coding: 2. Write a method which takes a list list of int , and reverse...
Java coding: 2. Write a method which takes a list list of int , and reverse it. // recursion 3.Write a method which takes a list list of strings , and reverse it. // in different way than the previous 3. Write a two methods which take a list and find the largest integer number in it.
Critics of the percentage-of-sales method of budget setting contend that this method “reverses the advertising and...
Critics of the percentage-of-sales method of budget setting contend that this method “reverses the advertising and sales relationship” and that it “treats advertising as an expense rather than an investment.” Explain what these arguments mean and discuss their merits
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT