Untitled
unknown
c_cpp
2 years ago
1.3 kB
3
Indexable
#include <iostream> #include <stack> class Stack { private: std::stack<char> items; public: bool isEmpty() { return items.empty(); } void push(char item) { items.push(item); } char pop() { char item = '\0'; // Default value for an empty stack if (!isEmpty()) { item = items.top(); items.pop(); } return item; } char peek() { char item = '\0'; // Default value for an empty stack if (!isEmpty()) { item = items.top(); } return item; } int size() { return items.size(); } }; Stack loadstack(int upper = 0) { Stack stack; char start = (upper == 1) ? 'A' : 'a'; char end = (upper == 1) ? 'Z' : 'z'; for (char ch = start; ch <= end; ++ch) { stack.push(ch); } return stack; } int main() { Stack lowercaseStack = loadstack(); Stack uppercaseStack = loadstack(1); // Test the stacks while (!lowercaseStack.isEmpty()) { std::cout << lowercaseStack.pop() << " "; } std::cout << std::endl; while (!uppercaseStack.isEmpty()) { std::cout << uppercaseStack.pop() << " "; } std::cout << std::endl; return 0; }
Editor is loading...