Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SIMPLE_QUEUE
- #define SIMPLE_QUEUE
- struct Node
- {
- private:
- int val;
- Node * left;
- Node * right;
- public:
- Node()
- {
- val = 0;
- left = 0;
- right = 0;
- }
- Node(int v)
- {
- val = v;
- left = 0;
- right = 0;
- }
- Node * GetLeftChild() {return left;}
- Node * GetRightChild() {return right;}
- int GetVal() {return val;}
- void SetVal(int v)
- {
- val = v;
- }
- void SetLeftChild(Node *node)
- {
- left = node;
- }
- void SetRightChild(Node *node)
- {
- right = node;
- }
- };
- class SimpleQueue
- {
- private:
- short int state; //0 - not initialized, 1 - not full, 2 - full, 3 - empty
- int elCount;
- int capacity;
- Node ** queue;
- int pushPos;
- int popPos;
- public:
- SimpleQueue()
- {
- elCount = 0;
- state = 0;
- capacity = 0;
- pushPos = 0;
- popPos = 0;
- }
- SimpleQueue(int n)
- {
- queue = new Node* [n];
- if (queue != 0)
- {
- elCount = 0;
- capacity = n;
- state = 3;
- pushPos = 0;
- popPos = 0;
- }
- else
- {
- state = 0;
- elCount = 0;
- capacity = 0;
- pushPos = 0;
- popPos = 0;
- }
- }
- bool init(int n)
- {
- if (state == 0)
- {
- queue = new Node* [n];
- if (queue != 0)
- {
- elCount = 0;
- capacity = n;
- state = 3;
- pushPos = 0;
- popPos = 0;
- return true;
- }
- return false;
- }
- }
- bool Push(Node *el)
- {
- if (state == 1 || state == 3)
- {
- queue[pushPos++] = el;
- state = (pushPos == capacity) ? 2 : 1;
- return true;
- }
- return false;
- }
- Node *Pop()
- {
- if (state != 0 && state != 3)
- {
- state = (popPos+1 == pushPos) ? 3 : 1;
- return queue[popPos++];
- }
- return 0;
- }
- int GetState() {return state;}
- int GetCapacity(){return capacity;}
- int GetElementCount(){return elCount;}
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment