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

In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
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...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
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,...
haskell : write a function that reverse the first three element of a list, but not...
haskell : write a function that reverse the first three element of a list, but not the rest. example [1,2,3,4,5,6] == [3,2,1,4,5,6]
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT