raxbg

SimpleQueue.h

May 9th, 2012
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #ifndef SIMPLE_QUEUE
  2. #define SIMPLE_QUEUE
  3.  
  4. struct Node
  5. {
  6.     private:
  7.         int val;
  8.         Node * left;
  9.         Node * right;
  10.     public:
  11.         Node()
  12.         {
  13.             val = 0;
  14.             left = 0;
  15.             right = 0;
  16.         }
  17.  
  18.         Node(int v)
  19.         {
  20.             val = v;
  21.             left = 0;
  22.             right = 0;
  23.         }
  24.  
  25.         Node * GetLeftChild() {return left;}
  26.         Node * GetRightChild() {return right;}
  27.         int GetVal() {return val;}
  28.  
  29.         void SetVal(int v)
  30.         {
  31.             val = v;
  32.         }
  33.  
  34.         void SetLeftChild(Node *node)
  35.         {
  36.             left = node;
  37.         }
  38.  
  39.         void SetRightChild(Node *node)
  40.         {
  41.             right = node;
  42.         }
  43. };
  44.  
  45. class SimpleQueue
  46. {
  47.     private:
  48.         short int state; //0 - not initialized, 1 - not full, 2 - full, 3 - empty
  49.         int elCount;
  50.         int capacity;
  51.         Node ** queue;
  52.         int pushPos;
  53.         int popPos;
  54.  
  55.     public:
  56.     SimpleQueue()
  57.     {
  58.         elCount = 0;
  59.         state = 0;
  60.         capacity = 0;
  61.         pushPos = 0;
  62.         popPos = 0;
  63.     }
  64.  
  65.     SimpleQueue(int n)
  66.     {
  67.         queue = new Node* [n];
  68.         if (queue != 0)
  69.         {
  70.             elCount = 0;
  71.             capacity = n;
  72.             state = 3;
  73.             pushPos = 0;
  74.             popPos = 0;
  75.         }
  76.         else
  77.         {
  78.             state = 0;
  79.             elCount = 0;
  80.             capacity = 0;
  81.             pushPos = 0;
  82.             popPos = 0;
  83.         }
  84.     }
  85.  
  86.     bool init(int n)
  87.     {
  88.         if (state == 0)
  89.         {
  90.             queue = new Node* [n];
  91.             if (queue != 0)
  92.             {
  93.                 elCount = 0;
  94.                 capacity = n;
  95.                 state = 3;
  96.                 pushPos = 0;
  97.                 popPos = 0;
  98.                 return true;
  99.             }
  100.             return false;
  101.         }
  102.     }
  103.  
  104.     bool Push(Node *el)
  105.     {
  106.         if (state == 1 || state == 3)
  107.         {
  108.             queue[pushPos++] = el;
  109.             state = (pushPos == capacity) ? 2 : 1;
  110.             return true;
  111.         }
  112.         return false;
  113.     }
  114.  
  115.     Node *Pop()
  116.     {
  117.         if (state != 0 && state != 3)
  118.         {
  119.             state = (popPos+1 == pushPos) ? 3 : 1;
  120.             return queue[popPos++];
  121.         }
  122.         return 0;
  123.     }
  124.  
  125.     int GetState() {return state;}
  126.  
  127.     int GetCapacity(){return capacity;}
  128.  
  129.     int GetElementCount(){return elCount;}
  130. };
  131. #endif
Advertisement
Add Comment
Please, Sign In to add comment