summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_2/data_structures/queue_w_array.cc
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-10-18 18:56:43 +0200
committerSanto Cariotti <dcariotti24@gmail.com>2020-10-20 09:08:52 +0200
commitf279107065146a4940f5e73602a1c3c09e58b31d (patch)
treefd892749637a8b6c5c31ccb80bba04ade76ab87a /I_anno/Programmazione_2/data_structures/queue_w_array.cc
parent4e063e32250312c38d5646840719b62429362b21 (diff)
chore: name of first year folder
Diffstat (limited to 'I_anno/Programmazione_2/data_structures/queue_w_array.cc')
-rw-r--r--I_anno/Programmazione_2/data_structures/queue_w_array.cc66
1 files changed, 0 insertions, 66 deletions
diff --git a/I_anno/Programmazione_2/data_structures/queue_w_array.cc b/I_anno/Programmazione_2/data_structures/queue_w_array.cc
deleted file mode 100644
index fb92d68..0000000
--- a/I_anno/Programmazione_2/data_structures/queue_w_array.cc
+++ /dev/null
@@ -1,66 +0,0 @@
-#include<iostream>
-
-using namespace std;
-
-template<class H>
-class queue {
-public:
- queue(int n) : _size{n}, _counter{0}, _head{0}, _tail{0} {
- _arr = new H[n];
- }
- ~queue() {
- delete _arr;
- }
- bool is_empty() {
- return _counter == 0;
- }
- bool is_full() {
- return _counter == _size;
- }
-
- queue<H>* enqueue(H x) {
- if(!is_full()) {
- _arr[_tail] = x;
- if(_tail == _size-1)
- _tail = 0;
- else
- ++_tail;
- _counter++;
- }
-
- return this;
- }
- H dequeue() {
- if(is_empty()) return -1;
- H x = _arr[_head];
-
- if(_head == _size-1)
- _head = 0;
- else
- ++_head;
-
- _counter--;
- return x;
- }
-private:
- H* _arr;
- int _size;
- short _counter;
- short _head;
- short _tail;
-};
-
-int main() {
- queue<int>* q = new queue<int>{4};
- q->enqueue(5)->enqueue(13)->enqueue(3);
- cout << q->dequeue() << '\n';
- q->enqueue(4)->enqueue(6)->enqueue(7);
- q->dequeue();
- q->enqueue(7);
-
- for(int i = 0; i < 6; ++i) {
- cout << q->dequeue() << ' ';
- }
- delete q;
- return 0;
-}