C++: The assignment is to create a dynamic implementation of a deque. This implementation uses the template linked list class from STL (#include ).
These are the files needed for the implementation;
The header file: deque.h
#ifndef _DEQUE_H_ #define _DEQUE_H_
#include #include #include
template class deque { public: deque(); //postcondition: empty deque has been created T& front(); // precondition: deque is not empty // postcondition: reference to element at front of deque // has been returned T front() const; // precondition: deque is not empty // postcondition: copy of element at front of deque // has been returned T& back(); // precondition: deque is not empty // returned: reference to element at front of deque T back() const; // precondition: deque is not empty // returned: copy of element at back of deque void push_front (const T& entry); // postcondition: entry has been inserted at the front // of the deque void push_back (const T& entry); // 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 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: std::list l; };
#include "deque.template"
#endif
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; }