Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Queue with Limited Size of Arrays
// Implement Queue with Limited Size of Arrays // https://www.cs.bu.edu/teaching/c/queue/array/types.html #include <vector> #include <iostream> using namespace std; class ListNode{ public: int fixNum; int used; int read; int write; vector<int> v; ListNode* next; ListNode* pre; public: ListNode(int num): fixNum(num), used(0), next(NULL), pre(NULL){ v = vector<int>(num,0); // read = write = 0; } void push(int val){ if(used >= fixNum) return; v[write] = val; used++; write = (write+1)%fixNum; } void pop() { if(used==0) return; used--; read = (read+1)%fixNum; // NOTE }; int front(){ return v[read]; }; bool empty(){ return used == 0; } bool full(){ return used == fixNum; } }; class QueueFixed{ public: int fixNum; int used; ListNode* front; ListNode* back; public: QueueFixed(int num): fixNum(num), used(0){ front = back = new ListNode(num); } void push(int v) { if(back->full()){ back->pre = new ListNode(fixNum); back->pre->next = back; back = back->pre; } back->push(v); used++; } void pop() { if(used ==0) return; if(front->empty()) { ListNode* p = front->pre; delete front; front = p; } front->pop(); used--; } int get_front(){ if(used == 0) return -1; return front->front(); } bool empty(){ return used == 0; } void print(){ cout << "used = " << used << endl; cout << "front->used = " << front->used << endl; cout << "back->used = " << back->used << endl; } }; int main() { QueueFixed q(3); q.push(1); q.push(2); q.push(3); q.print(); //testing push() q.push(4); q.push(5); q.print(); //test front(), pop() cout << "=> q.front() = "<< q.get_front() << endl; q.pop(); cout << "=> q.front() = "<< q.get_front() << endl; q.print(); }
run
|
edit
|
history
|
help
0
Gauss 4x4 reducido
Ploshtina na pravoagolnik
simple use of templete
test
Varadic macro
lab17feb22x4B.cpp
diamond
Grundy Number
String match with test
2021, M2, Simulare;S3: 2