diff --git a/Contributors.md b/Contributors.md index bb585a2..06a7a96 100644 --- a/Contributors.md +++ b/Contributors.md @@ -38,3 +38,4 @@ * [Muskan](https://github.com/Muskan-goyal6) * [Tarannum](https://github.com/giTan7) * [HCamberos](https://github.com/HCamberos) +* [Igor](https://github.com/IgorBizyanov) diff --git a/Queue/queue.cpp b/Queue/queue.cpp new file mode 100644 index 0000000..746f377 --- /dev/null +++ b/Queue/queue.cpp @@ -0,0 +1,121 @@ +#include +#include + +using namespace std; + +template +class CircularQueue { +public: + + CircularQueue(int k) { + size = k; + buffer = (T*)malloc(sizeof(T)*k); + head = -1; + tail = -1; + } + + /* + This function inserts an element into CircularQueue + Args: + - int value: value to be inserted + */ + bool enQueue(T value) { + if (isFull()){ + + return false; + } + cout << "DEBUG size == 0 or Full" << endl; + + if (isEmpty()) { + head = 0; + tail = 0; + } + + else + tail = (tail + 1) % size; + + buffer[tail] = value; + return true; + } + + /** Delete an element from the circular queue. Return true if the operation is successful. */ + bool deQueue() { + if (isEmpty()) + return false; + + buffer[head] = 0; + if (head == tail) { + head = tail = -1; + } + else + head = (head + 1) % size; + + return true; + } + + /** Get the front item from the queue. */ + T Head() { + return buffer[head]; + } + + /** Get the last item from the queue. */ + T Tail() { + return buffer[tail]; + } + + + void print() { + + for (int i = 0; i < size; ++i) { + cout << buffer[i] << " "; + } + cout << endl; + cout << "head = " << head << " tail = " << tail << endl; + } + +private: + + T *buffer; + int size; + int head; + int tail; + + bool isEmpty() { + return (head == -1 && tail == -1); + } + + bool isFull() { + return size == 0 || head == ((tail+1) % size); + } + +}; + +/*Usage: + en x : enqueue x number to the end of our queue + de : dequeue head number + +*/ +int main() { + string command; + double key; + CircularQueue qu = CircularQueue(6); + + while (true) { + + cin >> command; + if (command == "en") { + cin >> key; + qu.enQueue(key); + qu.print(); + } + if (command == "de") { + qu.deQueue(); + qu.print(); + } + + if (command == "exit") + break; + } + + return 0; +}