Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Queue {
- private:
- int front;
- int* buffer;
- int capacity;
- int size;
- public:
- Queue(int n) : capacity{ n }, front{ 0 }, size{ 0 } {
- buffer = new int[n];
- }
- ~Queue() {
- delete[] buffer;
- }
- int getSize() {
- return size;
- }
- bool isEmpty() {
- return size == 0;
- }
- bool isFull() {
- return size == capacity;
- }
- void enqueue(int val) {
- if (isFull()) {
- throw "already full";
- }
- buffer[(front + size) % capacity] = val;
- size += 1;
- }
- int peek() {
- return buffer[front];
- }
- void dequeue() {
- if (isEmpty()) {
- throw "already empty";
- }
- front = (front + 1) % capacity;
- size -= 1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment