Question

In: Computer Science

/* print_reverse function that prints the nodes in reverse order if the list is empty print...

/*

print_reverse function that prints the nodes in reverse order

if the list is empty print nothing

*/

include <bits/stdc++.h>

using namespace std;



class Node{

    public:

    int data;

    Node *next;

    Node *prev;

    Node(int d, Node *p=NULL, Node *n=NULL){

        data = d;

        prev = p;

        next = n;

    }

};

class DLL{

    public:

    Node *head;

    DLL(){head=NULL;}

    void push(int d){

        Node *pNode = new Node(d,NULL,head);

        if (head != NULL)

            head->prev = pNode;

        head = pNode;

    }

    void print_forward(){

        Node *pCurr = head;

        while (pCurr != NULL){

            cout << pCurr->data << endl;

            pCurr = pCurr->next;            

        }

    }

    void print_reverse(){

        // YOUR CODE HERE

    }

};

void main_body() {

    DLL myList;

    myList.push(2);

    myList.push(1);

    myList.push(5);

    myList.push(6);

    myList.print_reverse();

}

int main()

{

    main_body();

    return 0;

}

Solutions

Expert Solution

The function print_reverse() can be defined as follows:

  • Create a node tail and assign head to it.
  • Now using a while loop, traverse to the end of the list and find the tail of the list.
  • Now using another while, traverse the list in backwards from the tail using the previous pointer until the next of tail is null.
  • In the end, print the final node.

The C++ program with the function definition of function print_reverse() is as follows:

#include <bits/stdc++.h>
using namespace std;

//definition of class Node
class Node{

    public:

    int data;

    Node *next;

    Node *prev;

    Node(int d, Node *p=NULL, Node *n=NULL){

        data = d;

        prev = p;

        next = n;

    }

};

//definition of class linked list
class DLL{

    public:

    Node *head;

    DLL(){head=NULL;}

    void push(int d){

        Node *pNode = new Node(d,NULL,head);

        if (head != NULL)

            head->prev = pNode;

        head = pNode;

    }

    void print_forward(){

        Node *pCurr = head;

        while (pCurr != NULL){

            cout << pCurr->data << endl;

            pCurr = pCurr->next;            

        }

    }

    //function print_reverse()
    //this function will reverse the list in reverse
    //using the previous pointer traverse the list from tail
    void print_reverse()
    {
    Node* tail = head;   //initialize tail to head
    
    while (tail->next != NULL)    //find the tail
    { 
        tail = tail->next; 
    } 
  
    // Traversing linked list from tail to head (in reverse)
    while (tail != head) 
    { 
        cout << tail->data << " ";  //print the data
        tail = tail->prev;          //go backwards using previous pointer
    } 
    cout << tail->data << endl;     //in the end print the last

    }

};

void main_body() {

    DLL myList;

    myList.push(2);

    myList.push(1);

    myList.push(5);

    myList.push(6);
    
    myList.print_reverse();

}

int main()

{

    main_body();

    return 0;

}

The sample run:


Related Solutions

Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
The following code was meant to print out the elements in an array in reverse order....
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {         reverse(a, index); What does it do? Explain why it behaves in this way and There is more than one error in the code. Correct the code so that it will recursively print out the elements of...
reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse...
reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse all the numbers in the list and return a list with the reversed numbers. If a number ends with 0 you need to remove all the trailing zeros before reversing the number. An example of reversing numbers with trailing zeros: 10 -> 1, 590 -> 95. None of the numbers in the number_list will be less than 1. Example: number_list = [13, 45, 690,...
The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly.
  The following code was meant to print out the elements in an array in reverse order. However, it does not behave correctly. public static void reverse(int[] a, int index) {       if (index == (a.length - 1))         System.out.printf("%d%n", a[index]);       else {          reverse(a, index); What does it do? Explain why it behaves in this way.                                    There is more than one error in the code. Correct the code so that it will recursively print out...
Create the following java program with class list that outputs: //output List Empty List Empty List...
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is...
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(")...
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(") " + entry) return int(input(question)) plz explain this code
Write a function in any functional programming language that will reverse a general list. For example,...
Write a function in any functional programming language that will reverse a general list. For example, if an input is (A (B C (D E)) F), output is (F ((E D) C B) A).  Please note that any built-in/pre-defined function, e.g., reverse, cannot be used in your answer. Please DO NOT hard-code any input values, output values in your code. Please submit a screenshot of where your code got compiled, executed, showing the execution result
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...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print...
C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print Make your unorderedList a list of characters instead of integers Insert ten characters into the list and print it out both ways #include <iostream> #include <string> #include <cstdlib> using namespace std; struct node { int info; node* next; }; class unorderedList { private: int length; node* listPtr; public: unorderedList() {length = 0; listPtr = NULL;} void makeEmpty(); void insertItem(int item); void printList(); bool isFull()...
public java.util.ArrayList<T> getReverseArrayList() Returns an ArrayList with the element of the linked list in reverse order....
public java.util.ArrayList<T> getReverseArrayList() Returns an ArrayList with the element of the linked list in reverse order. This method must be implemented using recursion. public BasicLinkedList<T> getReverseList() Returns a new list with the elements of the current list in reverse order. You can assume sharing of data of each node is fine. This method must be implemented using recursion. JAVA 8.5.2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT