Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // Module Name: int_stack.h/cpp
- // Authors: Sergey Shershakov
- // Version: 0.2.0
- // Date: 23.01.2017
- //
- // This is a part of the course "Algorithms and Data Structures"
- // provided by the School of Software Engineering of the Faculty
- // of Computer Science at the Higher School of Economics.
- ////////////////////////////////////////////////////////////////////////////////
- #include "int_stack.h"
- #include <stdexcept>
- // TODO: add necessary headers here
- // #include <...
- namespace xi {
- /** Declares a stack of integers.
- */
- //----<Main ADT interface>----
- IntStack::IntStack(size_t sz)
- {
- _stack = new int[sz];
- _ssize = sz;
- _head = 0;
- }
- IntStack::~IntStack()
- {
- delete[] _stack;
- _ssize = 0;
- _head = 0;
- }
- /** Pushes a given element onto the stack.
- *
- * If no more elements can be placed onto the stack due to its overflow an std::logic_error is thrown.
- */
- void IntStack::push(int el)
- {
- if (isFull())
- throw std::logic_error("The stack is full.");
- _stack[_head++] = el;
- }
- /** Pops an element from the stack and returns its value.
- *
- * If no elements stored in the stack, a std::logic_error is thrown.
- */
- int IntStack::pop()
- {
- if (isEmpty())
- throw std::logic_error("The stack is empty.");
- return _stack[--_head];
- }
- /** Looks up an element from the top of the stack and returns its value.
- *
- * If no elements stored in the stack, a std::logic_error is thrown.
- */
- int IntStack::top()
- {
- if (isEmpty())
- throw std::logic_error("The stack is empty.");
- return _stack[_head - 1];
- }
- /** Clears the stack and makes it empty.
- *
- * Often named as makeEmpty or similar
- */
- void IntStack::clear()
- {
- delete[] _stack;
- _stack = new int[_ssize];
- _head = 0;
- }
- // Non classic ADT helper methods
- /** Returns true if the stack is empty, false otherwise */
- bool IntStack::isEmpty() const
- {
- if (_head == 0)
- return true;
- return false;
- }
- /** Returns true if the stack is full, false otherwise */
- bool IntStack::isFull() const
- {
- if (_head == _ssize)
- return true;
- return false;
- }
- } // namespace xi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement