C++: I need to create a static implementation of a deque
Here is the header file: deque.h
#ifndef _DEQUE_H_ #define _DEQUE_H_ #include #include template class deque { public: static const size_t CAPACITY = 10; deque(); //postcondition: empty deque has been created T& front(); //precondition: deque is not empty // returned: reference to element at front of deque T front() const; // precondition: deque is not empty // returned: copy of element at front of deque T& back(); // precondition: deque is not empty // returned: reference to element at back of deque T back() const; // precondition: deque is not empty // returned: copy of element at back of deque void push_front (const T& entry); // precondition: deque is not full // postcondition: entry has been inserted at the front // of the deque void push_back (const T& entry); // precondition: deque is not full // postcondition: entry has been inserted at the back // of the deque void pop_front(); // precondition: deque is not empty // postcondition: element at front of deque has been removed void pop_back(); // precondition: deque is not empty // postcondition: element at back of deque has been removed size_t size() const; // postcondition: number of elements in deque has been returned bool empty() const; // returned: whether deque is empty bool full() const; // returned: whether deque is full has been returned template friend bool operator == (const deque& dq1, const deque& dq2); // returned: whether 2 deques are equal - equal is defined // as the deques have the same number of elements & // corresponding elements are equal template friend std::ostream& operator<< (std::ostream& out, const deque& dq); // postcondition: dq has been display from front to rear on out private: T data[CAPACITY]; // Circular array size_t first; // Index of item at front of the queue size_t last; // Index of item at rear of the queue size_t count; // Total number of items in the queue size_t next_index(size_t i) const; // returned: next index in array size_t prev_index (size_t i) const; // returned: previous index in array }; #include "deque.template" #endif
Here is the test file: test_deque.cpp
#include #include "deque.h" using namespace std; int main(int argc, char **argv) { deque dq; cout << dq << endl; for (int i = 0; i < 10; i++) dq.push_front (i); cout << dq << endl; dq.pop_back(); cout << dq << endl; dq.push_front (24); cout << dq << endl; return 0; }