Advertisement
jtentor

DemoStack1 - Cpp - stack.cpp

May 9th, 2020
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. //
  2. // Created by Julio Tentor <jtentor@fi.unju.edu.ar>
  3. //
  4.  
  5. #include "stack.h"
  6. #include <stdexcept>
  7.  
  8.  
  9. stack::stack(int capacidad) {
  10.     this->capacidad = capacidad;
  11.     this->datos = new char[this->capacidad];
  12.     this->cuenta = 0;
  13. }
  14.  
  15. stack::~stack() {
  16.     delete [] this->datos;
  17. }
  18.  
  19. void stack::push(const char elemento) {
  20.     if (this->cuenta >= this->capacidad) {
  21.         throw std::runtime_error("La pila está llena...");
  22.     }
  23.     this->datos[this->cuenta] = elemento;
  24.     ++this->cuenta;
  25. }
  26.  
  27. char stack::pop() {
  28.     if (this->empty()) {
  29.         throw std::runtime_error("ERROR La pila esta vacía...");
  30.     }
  31.     --this->cuenta;
  32.     return this->datos[this->cuenta];
  33. }
  34.  
  35. char stack::peek() {
  36.     if (this->empty()) {
  37.         throw std::runtime_error("ERROR La pila esta vacía...");
  38.     }
  39.     return this->datos[this->cuenta - 1];
  40. }
  41.  
  42. bool stack::empty() {
  43.     return this->cuenta <= 0;
  44. }
  45.  
  46. int stack::count() {
  47.     return this->cuenta;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement