Advertisement
Guest User

stack.cpp

a guest
Oct 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Module Name:  int_stack.h/cpp
  3. // Authors:      Sergey Shershakov
  4. // Version:      0.2.0
  5. // Date:         23.01.2017
  6. //
  7. // This is a part of the course "Algorithms and Data Structures"
  8. // provided by  the School of Software Engineering of the Faculty
  9. // of Computer Science at the Higher School of Economics.
  10. ////////////////////////////////////////////////////////////////////////////////
  11.  
  12.  
  13. #include "int_stack.h"
  14. #include <stdexcept>
  15.  
  16. // TODO: add necessary headers here
  17. // #include <...
  18.  
  19. namespace xi {
  20.  
  21.     /** Declares a stack of integers.
  22.      */
  23.      //----<Main ADT interface>----
  24.  
  25.     IntStack::IntStack(size_t sz)
  26.     {
  27.         _stack = new int[sz];
  28.         _ssize = sz;
  29.         _head = 0;
  30.     }
  31.  
  32.     IntStack::~IntStack()
  33.     {
  34.         delete[] _stack;
  35.         _ssize = 0;
  36.         _head = 0;
  37.     }
  38.  
  39.     /** Pushes a given element onto the stack.
  40.       *
  41.       *  If no more elements can be placed onto the stack due to its overflow an std::logic_error is thrown.
  42.       */
  43.     void IntStack::push(int el)
  44.     {
  45.         if (isFull())
  46.             throw std::logic_error("The stack is full.");
  47.         _stack[_head++] = el;
  48.     }
  49.  
  50.     /** Pops an element from the stack and returns its value.
  51.      *
  52.      *  If no elements stored in the stack, a std::logic_error is thrown.
  53.      */
  54.     int IntStack::pop()
  55.     {
  56.         if (isEmpty())
  57.             throw std::logic_error("The stack is empty.");
  58.         return _stack[--_head];
  59.     }
  60.  
  61.     /** Looks up an element from the top of the stack and returns its value.
  62.      *
  63.      *  If no elements stored in the stack, a std::logic_error is thrown.
  64.      */
  65.     int IntStack::top()
  66.     {
  67.         if (isEmpty())
  68.             throw std::logic_error("The stack is empty.");
  69.         return _stack[_head - 1];
  70.     }
  71.  
  72.     /** Clears the stack and makes it empty.
  73.      *
  74.      *  Often named as makeEmpty or similar
  75.      */
  76.     void IntStack::clear()
  77.     {
  78.         delete[] _stack;
  79.         _stack = new int[_ssize];
  80.         _head = 0;
  81.     }
  82.  
  83.     // Non classic ADT helper methods
  84.  
  85.     /** Returns true if the stack is empty, false otherwise */
  86.     bool IntStack::isEmpty() const
  87.     {
  88.         if (_head == 0)
  89.             return true;
  90.         return false;
  91.     }
  92.  
  93.     /** Returns true if the stack is full, false otherwise */
  94.     bool IntStack::isFull() const
  95.     {
  96.         if (_head == _ssize)
  97.             return true;
  98.         return false;
  99.     }
  100.  
  101. } // namespace xi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement