Problem with a reverse function in a template class stack implementation
Jul 10, 2014 at 11:47pm UTC
this is my stack implementation using template with a struct type node and a class type stack:
Stack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#ifndef STACK_H_
#define STACK_H_
#include <cstdlib>
#include <iostream>
#include <cassert>
using namespace std;
template <class t>
struct node{
t data;
node<t>* next;
};
template <class t>
class stack
{
public :
stack();
~stack();
bool isEmpty(){ return (top_ptr=NULL);};
void push(const t&);
void pop();
t top() const ;
void reverse();
void clear();
void print();
private :
node<t>* top_ptr;
};
template <class t>
stack<t>::stack()
{
top_ptr=NULL;
}
template <class t>
stack<t>::~stack()
{
while (top_ptr != NULL) pop();
}
template <class t>
void stack<t>::push(const t& source)
{
node<t>* new_node = new node<t>;
new_node->data = source;
new_node->next = top_ptr;
top_ptr = new_node;
cout << "Inserito!" << endl;
}
template <class t>
void stack<t>::pop()
{
node<t>* remove = top_ptr;
top_ptr = top_ptr->next;
delete remove;
cout << "Rimosso!" << endl;
}
template <class t>
t stack<t>::top() const
{
assert(top_ptr != NULL);
return top_ptr->data;
}
template <class t>
void stack<t>::clear()
{
node<t>* temp;
while (top_ptr != NULL)
{
temp = top_ptr;
top_ptr = top_ptr->next;
delete temp;
}
cout << "Clear completato!" << endl;
}
template <class t>
void stack<t>::reverse()
{
stack<t> new_stack;
while (top_ptr != NULL)
{
new_stack.push(top_ptr->data);
pop();
}
cout << "Reverse completato!" << endl;
}
template <class t>
void stack<t>::print()
{
node<t>* ptr = top_ptr;
while (ptr!=NULL)
{
cout << " " << ptr->data << endl;
ptr = ptr->next;
}
}
#endif /* STACK_H_ */
And this is the main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include "stack.h"
int main()
{
stack<int > stackino;
for (int i = 0; i<10; i++) stackino.push(i);
stackino.pop();
cout << "top(): " << stackino.top() << endl;
stackino.print();
cout << "Invoco clear()" << endl;
stackino.clear();
cout << "Stackino dopo clear():" << endl;
stackino.print();
cout << "Invoco reverse()" << endl;
stackino.reverse();
cout << "Stackino dopo reverse()" << endl;
stackino.print();
cout << "FINE!" << endl;
return 0;
}
The problem is reverse() that causes a crash of the program, I guess "top_ptr = new_stack.top_ptr" is wrong but it makes compile and execute, but crashes. Someone can help me correct this?
Thank you very much!
Jul 11, 2014 at 12:02am UTC
When does it crash? It doesn't crash for me
https://ideone.com/iA02LO
output:
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Inserito!
Rimosso!
top(): 8
8
7
6
5
4
3
2
1
0
Invoco clear()
Clear completato!
Stackino dopo clear():
Invoco reverse()
Reverse completato!
Stackino dopo reverse()
FINE!
Jul 11, 2014 at 12:45pm UTC
Sorry I forgot an instruction in the reverese() function, try this:
1 2 3 4 5 6 7 8 9 10 11 12
template <class t>
void stack<t>::reverse()
{
stack<t> new_stack;
while (top_ptr != NULL)
{
new_stack.push(top_ptr->data);
pop();
}
cout << "Reverse completato!" << endl;
top_ptr = new_stack.top_ptr;
}
I want to make the old top_ptr as the new_stack.top_ptr, but when I try to print the stack, it print nothing
Last edited on Jul 11, 2014 at 12:58pm UTC
Topic archived. No new replies allowed.