Advertisement
tomasaccini

Untitled

Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <mutex>
  2. #include <iostream>
  3.  
  4. class Pila_Enteros {
  5. private:
  6.     int arr[1000];
  7.     size_t cantidad_elementos;
  8.     std::mutex m;
  9. public:
  10.     Pila_Enteros(){
  11.         for (size_t i = 0; i < 1000; i++){
  12.             arr[i] = 0;
  13.         }
  14.         cantidad_elementos = 0;
  15.     }
  16.  
  17.     void push(int valor){
  18.         m.lock();
  19.         if (cantidad_elementos < 1000) {
  20.             arr[cantidad_elementos] = valor;
  21.             cantidad_elementos++;
  22.         }
  23.         m.unlock();
  24.     }
  25.  
  26.     int pop(){
  27.         m.lock();
  28.         if (cantidad_elementos == 0) {
  29.             m.unlock();
  30.             throw "Pila vacía";
  31.         }
  32.         int temp = arr[cantidad_elementos - 1];
  33.         --cantidad_elementos;
  34.         m.unlock();
  35.         return temp;
  36.     }
  37.  
  38.     size_t obtener_cantidad_elementos() {
  39.         return cantidad_elementos;
  40.     }
  41. };
  42.  
  43. int main(){
  44.     Pila_Enteros p;
  45.     p.push(10);
  46.     p.push(30);
  47.     std::cout << p.pop();
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement