Hi,
I've got such class template:
template <class Type> class Stack{
private:
struct Element{
Type value;
Element *prev;
}
*end;
public:
Stack(){
end = NULL;
}
void push(Type value){
struct Element *element = new Element;
element->value = value;
element->prev = end;
end = element;
}
Type pop(){
if(end != NULL){
Type tempValue = end->value;
struct Element *tempElement = end->prev;
end = NULL;
delete end;
end = tempElement;
return tempValue;
}
}
~Stack(){
while(end != NULL){
pop();
}
}
};
It works good. But my primary assumption was to create stack which can store mixed types of data, f.e.
// now i have
Stack<int> stack; // i have to declare what kind of type will store the stack
stack.push(4);
// i would i have
Stack stack;
stack.push(4);
stack.push(3.14);
stack.push(string);
stack.push('c');
// etc
I didn't have idea how to do this so I've written simplified version of stack.
I was looking for some concept on web but unfortunately didn't find nothing especially.