rihardmarius

colas con listas

Nov 22nd, 2013
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // colas con listas
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. struct node {
  8.     int entry = 0;
  9.     node* next = nullptr;
  10. };
  11.  
  12. struct queue {
  13.     node* front = nullptr;
  14.     node* rear = nullptr;
  15. };
  16.  
  17. bool is_empty (const queue& q)
  18. {
  19.     return q.front == nullptr;
  20. }
  21.  
  22. void enqueue (queue& q, int v)
  23. {
  24.     node* nuevo = new node;
  25.     nuevo->entry = v;
  26.  
  27.     if(q.rear != 0) // apunta a algo?
  28.         q.rear->next = nuevo;
  29.  
  30.     q.rear = nuevo;
  31.  
  32.     if (q.front == 0)
  33.         q.front = nuevo;
  34. }
  35.  
  36. void dequeue (queue& q, int& v)
  37. {
  38.     v = q.front->entry;
  39.     node* aux = q.front;
  40.     q.front = q.front->next;
  41.     if (q.front == 0)
  42.         q.rear = 0;
  43.     delete aux;
  44. }
  45.  
  46. int main()
  47. {
  48.     queue q; int a;
  49.  
  50.     for (int i = 0; i<10; i++)
  51.         enqueue(q, i+1);
  52.  
  53.     enqueue(q, 5);
  54.  
  55.     while (q.front != 0)
  56.     {
  57.         dequeue(q, a);
  58.         cout << a << endl;
  59.     }
  60.     return 0;
  61. }
RAW Paste Data