rihardmarius

libreria - pilas y colas con array

Nov 23rd, 2013
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <array>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct pila {
  7.     array<int,10> arr;
  8.     int count = 0;
  9. };
  10.  
  11. struct cola {
  12.     array <int,5> arr;
  13.     int front = 0;
  14.     int rear = 0;
  15.     int cant = 0;
  16. };
  17.  
  18. pila& push (pila& p, int v)
  19. {
  20.     p.arr.at(p.count) = v;
  21.     p.count++;
  22.     return p;
  23. }
  24.  
  25. pila& pop (pila& p, int& v)
  26. {
  27.     v = p.arr.at(p.count - 1);
  28.     p.count--;
  29.     return p;
  30. }
  31.  
  32. cola& enqueue (cola& q, int v)
  33. {
  34.     if (q.cant == 5)
  35.     {
  36.         cout << "Cola llena." << endl;
  37.         return q;
  38.     }
  39.     else
  40.     {
  41.         q.arr.at(q.rear) = v;
  42.         q.rear = (q.rear+1)%5;
  43.         q.cant++;
  44.         return q;
  45.     }
  46. }
  47.  
  48. cola& dequeue (cola& q, int& v)
  49. {
  50.     if (q.cant == 0)
  51.     {
  52.         cout << "Cola vacia." << endl;
  53.         return q;
  54.     }
  55.     else
  56.     {
  57.         v = q.arr.at(q.front);
  58.         q.front = (q.front+1)%5;
  59.         q.cant--;
  60.         return q;
  61.     }
  62. }
  63.  
  64. void mostrar_cola (cola& q)
  65. {
  66.     cout << "F a R: ";
  67.     for (int i=0; i<q.cant; i++)
  68.         cout << q.arr.at((q.front+i)%5) << ' ';
  69.     cout << endl;
  70. }
RAW Paste Data