Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Dequeue Array-Based Example
#include <iostream> #include <cassert> using namespace std; template <typename T> class Dequeue{ private: const int MAX_ITEMS = 100; T *items; int frontDequeue; int backDequeue; int itemCount; int maxItems; public: Dequeue(); /** Returns true if the deque is empty, else false */ bool isEmpty() const noexcept; /** Add item to the front of the deque * may throw std::bad_alloc */ void pushFront(const T & item); /** Remove the item at the front of the deque * throws std::runtime_error if the deque is empty */ void popFront(); /** Returns the item at the front of the deque * throws std::runtime_error if the deque is empty */ T front() const; /** Add item to the back of the deque * may throw std::bad_alloc */ void pushBack(const T & item); /** Remove the item at the back of the deque * throws std::runtime_error if the deque is empty */ void popBack(); /** Returns the item at the back of the deque * throws std::runtime_error if the deque is empty */ T back() const; }; template<class T> Dequeue<T>::Dequeue() : itemCount(0), maxItems(MAX_ITEMS), frontDequeue(1), backDequeue(MAX_ITEMS-1) { items = new T[MAX_ITEMS]; } // end default constructor template<class T> bool Dequeue<T>::isEmpty() const noexcept { return (itemCount==0); } template<class T> void Dequeue<T>::pushFront(const T & item) { frontDequeue = (MAX_ITEMS + frontDequeue - 1) % MAX_ITEMS; items[frontDequeue] = item; itemCount++; } template<class T> void Dequeue<T>::popFront() { frontDequeue = (frontDequeue+1)%MAX_ITEMS; itemCount--; } template<class T> T Dequeue<T>::front() const { return(items[frontDequeue]); } template<class T> void Dequeue<T>::pushBack(const T & item) { } template<class T> void Dequeue<T>::popBack() { } template<class T> T Dequeue<T>::back() const { } int main() { int testSize = 10; Dequeue<int> a; // stack tests for (int i=1; i<testSize; i++) { a.pushFront(i); } for (int i=testSize-1; i>0; i--) { assert(i==a.front()); a.popFront(); } assert(a.isEmpty()); cout << "Dequeue passed all my stack tests! Yay! " << endl; // queue tests for (int i=1; i<testSize; i++) { a.pushFront(i); } for (int i=1; i<testSize; i++) { assert(i==a.back()); a.popBack(); } assert(a.isEmpty()); cout << "Dequeue passed all my queue tests! Yay! " << endl; return 0; }
run
|
edit
|
history
|
help
0
Calculate H.C.F using recursion
test
ArrayList
Variable declarations in while loop conditions are also C++-only.
Heap: insert and deleteMin() Implementations
DESim Example Starter Code
First test
Task on Задача C. Белочка
Balanced Insert Example
10 naturalnumbers