C++/Generic/Generic Stack
A generic stack that holds two values.
<source lang="cpp">
- include <iostream>
using namespace std;
- define SIZE 10
template <class StackType> class stack {
StackType stck[SIZE][2]; int topOfStack;
public:
void init() { topOfStack = 0; } void push(StackType ob, StackType object2); StackType pop(StackType &object2);
}; template <class StackType> void stack<StackType>::push(StackType ob, StackType object2) {
if(topOfStack==SIZE) { cout << "Stack is full.\n"; return; } stck[topOfStack][0] = ob; stck[topOfStack][1] = object2; topOfStack++;
} template <class StackType> StackType stack<StackType>::pop(StackType &object2) {
if(topOfStack==0) { cout << "Stack is empty.\n"; return 0; } topOfStack--; object2 = stck[topOfStack][1]; return stck[topOfStack][0];
} int main() {
stack<char> stack1, stackObject2; int i; char ch; stack1.init(); stackObject2.init(); stack1.push("a", "b"); stackObject2.push("x", "z"); stack1.push("b", "d"); stackObject2.push("y", "e"); stack1.push("c", "a"); stackObject2.push("z", "x"); for(i = 0; i <3; i++) { cout << "Pop stack1: " << stack1.pop(ch); cout << " " << ch << endl; } for(i = 0; i <3; i++) { cout << "Pop stackObject2: " << stackObject2.pop(ch); cout << " " << ch << endl; } // demonstrate double stacks stack<double> doubleValueStack1, doubleValueStack2; // create two stacks double d; doubleValueStack1.init(); doubleValueStack2.init(); doubleValueStack1.push(1.1, 2.0); doubleValueStack2.push(2.2, 3.0); doubleValueStack1.push(3.3, 4.0); doubleValueStack2.push(4.4, 5.0); doubleValueStack1.push(5.5, 6.0); doubleValueStack2.push(6.6, 7.0); for(i = 0; i <3; i++) { cout << "Pop doubleValueStack1: " << doubleValueStack1.pop(d); cout << " "<< d << endl; } for(i = 0; i <3; i++) { cout << "Pop doubleValueStack2: " << doubleValueStack2.pop(d); cout << " "<< d << endl; } return 0;
}
</source>
A generic stack that includes exception handling.
<source lang="cpp">
- include <iostream>
using namespace std;
- define SIZE 10
template <class StackType> class stack {
StackType stck[SIZE]; int topOfStack;
public:
void init() { topOfStack = 0; } void push(StackType ch); StackType pop();
}; template <class StackType> void stack<StackType>::push(StackType ob) {
try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout << "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++;
} template <class StackType> StackType stack<StackType>::pop() {
try { if( topOfStack == 0) throw 0; } catch(int) { cout << "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack];
} int main() {
stack<char> stack1, stack2; int i; stack1.init(); stack2.init(); stack1.push("a"); stack2.push("x"); stack1.push("b"); stack2.push("y"); stack1.push("c"); stack2.push("z"); for(i = 0; i <3; i++) cout << "Pop stack1: " << stack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop stack2: " << stack2.pop() << endl; // demonstrate double stacks stack<double> doubleValueStack1, doubleValueStack2; // create two stacks // initialize the stacks doubleValueStack1.init(); doubleValueStack2.init(); doubleValueStack1.push(1.1); doubleValueStack2.push(2.2); doubleValueStack1.push(3.3); doubleValueStack2.push(4.4); doubleValueStack1.push(5.5); doubleValueStack2.push(6.6); for(i = 0; i <3; i++) cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl; return 0;
}
</source>