Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume there is a Stock class which has symbol (string), cost (int) and shares (

ID: 3814568 • Letter: A

Question

Assume there is a Stock class which has symbol (string), cost (int) and shares (int) as private members. There is also a StockNode class which inherits Stock class. Further assume there are classes for storing collection of Stock as follows:

DBfwd.. stores stocks in a forward linked list

DBdlink.. stores stocks in a doubly-linked list

DBstack .. stores stocks in a stack

Write a recursive reverse_print function for DBfwd assuming first is the only data members of the list. You can also assume Stock has operator << overload and StockNode is a Stock.

void DBfwd::reverse_print() {

Explanation / Answer

// C program to print reverse of a linked list

#include<stdio.h>

#include<stdlib.h>

  

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

  

/* Function to reverse the linked list */

void printReverse(struct node* head)

{

    // Base case

    if (head == NULL)

       return;

    // print the list after head node

    printReverse(head->next);

    // After everything else is printed, print head

    printf("%d ", head->data);

}

  

/*UTILITY FUNCTIONS*/

/* Push a node to linked list. Note that this function

  changes the head */

void push(struct node** head_ref, char new_data)

{

    /* allocate node */

    struct node* new_node =

            (struct node*) malloc(sizeof(struct node));

  

    /* put in the data */

    new_node->data = new_data;

  

    /* link the old list off the new node */

    new_node->next = (*head_ref);  

  

    /* move the head to pochar to the new node */

    (*head_ref)    = new_node;

}

  

/* Drier program to test above function*/

int main()

{

    // Let us create linked list 1->2->3->4

    struct node* head = NULL;   

    push(&head, 4);

    push(&head, 3);

    push(&head, 2);

    push(&head, 1);

   

    printReverse(head);

    return 0;

}

// C program to print reverse of a linked list

#include<stdio.h>

#include<stdlib.h>

  

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

  

/* Function to reverse the linked list */

void printReverse(struct node* head)

{

    // Base case

    if (head == NULL)

       return;

    // print the list after head node

    printReverse(head->next);

    // After everything else is printed, print head

    printf("%d ", head->data);

}

  

/*UTILITY FUNCTIONS*/

/* Push a node to linked list. Note that this function

  changes the head */

void push(struct node** head_ref, char new_data)

{

    /* allocate node */

    struct node* new_node =

            (struct node*) malloc(sizeof(struct node));

  

    /* put in the data */

    new_node->data = new_data;

  

    /* link the old list off the new node */

    new_node->next = (*head_ref);  

  

    /* move the head to pochar to the new node */

    (*head_ref)    = new_node;

}

  

/* Drier program to test above function*/

int main()

{

    // Let us create linked list 1->2->3->4

    struct node* head = NULL;   

    push(&head, 4);

    push(&head, 3);

    push(&head, 2);

    push(&head, 1);

   

    printReverse(head);

    return 0;

}