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'

Solutions

Expert Solution

C++ PROGRAM

#include<iostream>
using namespace std;

// create class for Node
class Node
{
   public:
       // public data members
   char data;
   Node *next,*prev; // reference pointers
};

// implement insert() function with two parameters
// first parameter is Node pointer object head
// second parameter character arguement
void insert(Node **head,char x)
{
   Node *temp=new Node(); // create pointer temp object
  
   temp->data=x; // assign x value to temp object
   temp->prev=NULL; // assign temp->prev=NULL
   temp->next=*head; // assign temp->next=head
  
   if(*head!=NULL) // check head!=NULL
   (*head)->prev=temp; // then, assign head->prev=temp
  
   *head=temp; // finally assign head=temp
}

// implement display() function with single argument

void display(Node *mynode)
{
   Node *last; // declare pointer object last
  
   while(mynode!=NULL) // create while loop mynode is not null
   {
       cout<<mynode->data<<" "; // display mynode data
       last=mynode; // assign mynode to last
       mynode=mynode->next; // repeat mynode->next until NULL
   }
}

// implement reverse() function with single argument
// return pointer Node type
Node * reverse(Node *head)
{
if(head==NULL) // check head=NULL
return head; // then, return head node
Node *temp=head->prev; // create pointer temp node and assign head->prev
while(head) // create while loop until head=NULL
{
   head->prev=head->next; // assign head->next to head->prev
   head->next=temp; // assign temp node to head->next
   temp=head; // assign head to temp
   head=head->prev; // repeate head->prev until NULL
   }
   return temp; // return temp node
  
}

int main()
{
   Node *head=NULL; // create pointer node head and assign NULL
  
   // calling insert() function and insert values into head node
   insert(&head,'e');
   insert(&head,'d');
   insert(&head,'c');
   insert(&head,'b');
   insert(&head,'a');
  
   cout<<"The given list is"<<endl;
   display(head); // calling display() function and display original list
  
   Node *t=reverse(head); // calling reverse() function and receive return Node list
   cout<<"\nThe return list should be"<<endl;
   display(t); // calling display() function and display reverse order list
  
  
  
   return 0;
}

OUTPUT-1

The given list is
a b c d e
The return list should be
e d c b a

OUTPUT-2

If insert two more elements then,

insert(&head,'g');
   insert(&head,'f');
   insert(&head,'e');
   insert(&head,'d');
   insert(&head,'c');
   insert(&head,'b');
   insert(&head,'a');

The given list is
a b c d e f g
The return list should be
g f e d c b a


Related Solutions

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