c need help thanks One of the problems of our Stack and Link

c++ need help! thanks.

One of the problems of our Stack and LinkedList classes is that if we make them handle generic types with void* pointers, they will not know how to delete the elements we insert in them. We will see later how templates will solve that, but for now let\'s practice using pointers to functions.

Extend the book\'s Stack struct with one more member variable that will hold a pointer to the following function prototype:

void deletecb (void * pt);

You will then add a member function to set this pointer:

void Stack::setDeleteCallback ( void (*delcb) (void * pt) );

After you do this, then add the corresponding code in the cleanup method to traverse all elements of the stack, deleting the links and calling the delete callback once for each stored void pointer. The user will be responsible for providing the delete callback and implementing it with the correct delete call after converting the void pointer argument to its correct type.

Below is the information code:

You should test above code with:

So sample input would be :

3
1.1
2.2
3.3

and output is :

Deleting: 3.3
Deleting: 2.2
Deleting: 1.1

Solution

void Stack::initialize() { head = 0; }

void Stack::push(void* dat) {

Link* newLink = new Link();

newLink->initialize(dat, head);

head = newLink;}

void* Stack::peek() { return head->data; }

void* Stack::pop() {

if(head == 0) return 0;

void* result = head->data;

Link* oldHead = head;

head = head->next;

delete oldHead;

return result;}

void Stack::cleanup() {

Link* cursor = head;

while(head) {

    cursor = cursor->next;

    delete head->data; delete head;

    head = cursor;

  }

head = 0;}

void Stack::setDeleteCallback ( void (*delcb) (void * pt) );{

void deletecb (void * pt);

{

double *dpt=(double *pt);

cout<<”Deleting books:<<dpt<<”\ ”;

delete dpt;}}

Main function:

#include <iostream>

#include \"Stack.h\"

using namespace std;

void delete_func(void * pt){

    double * dpt = (double *)pt;

    cout<<\"Deleting: \" << * dpt << \"\ \";

    delete dpt;}

int main(int argc, const char * argv[]) {

    Stack * doubleStack = new Stack;

doubleStack->initialize();

double value;

int n;

cin >> n;

for (int i=0; i < n; i++){

cin >> value;

        doubleStack->push(new double(value));

        } void (*del_func_ptr)(void * pt) = delete_func;

    doubleStack->setDeleteCallback(del_func_ptr);

doubleStack->setDeleteCallback(deletecb);

    doubleStack->cleanup();

    return 0;}

c++ need help! thanks. One of the problems of our Stack and LinkedList classes is that if we make them handle generic types with void* pointers, they will not k
c++ need help! thanks. One of the problems of our Stack and LinkedList classes is that if we make them handle generic types with void* pointers, they will not k
c++ need help! thanks. One of the problems of our Stack and LinkedList classes is that if we make them handle generic types with void* pointers, they will not k

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site