Abstract Data Types (ADT)
This page is under construction. Please come back later.
/****************************************************************************** * This program demonstrates the Stack ADT * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ #include "Utils.hpp" #include <clocale> #include <codecvt> #include <iostream> #include <memory> #include <string> #include <vector> std::locale utf8loc(std::locale(), new std::codecvt_utf8<wchar_t>); using namespace std; class IntegerStack { private: // Internal representation for the stack is a list vector<int> stack = {}; public: IntegerStack() noexcept { stack = {}; } int pop() { if (stack.size() == 0) { throw runtime_error(Utils::wchar_to_UTF8(L"Can't pop on empty stack!")); } return Utils::pop(stack); } void push(int value) noexcept { Utils::push(stack, value); } int peek() { if (stack.size() == 0) { throw runtime_error(Utils::wchar_to_UTF8(L"Can't peek on empty stack!")); } return stack[stack.size() - 1]; } }; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); shared_ptr<IntegerStack> stack(new IntegerStack()); for (auto x = 1; x <= 10; ++x) { stack->push(x); } try { wcout << stack->peek() << endl; for (auto x = 1; x <= 11; ++x) { wcout << stack->pop() << endl; } } catch (runtime_error ex) { wcout << Utils::exceptionMessage<wchar_t>(ex) << endl; } return 0; }
Output
$ g++ -std=c++17 ADTStack.cpp -o ADTStack -lfmt
$ ./ADTStack
10
10
9
8
7
6
5
4
3
2
1
St13runtime_error: Can't pop on empty stack!
/****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { cout << "Hello, world!" << endl; return 0; }
Output
$ g++ -std=c++17 HelloWorld.cpp -o HelloWorld -lfmt
$ ./HelloWorld
Hello, world!
/****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #include <iostream> #include <string> using namespace std; int main(int argc, char **argv) { cout << "Hello, world!" << endl; return 0; }
Output
$ g++ -std=c++17 HelloWorld.cpp -o HelloWorld -lfmt
$ ./HelloWorld
Hello, world!
Questions
Projects
More ★'s indicate higher difficulty level.
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]