Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix the deque.template file so that the test program runs correctly. My program: deque.template //File: deque.template //This program is a static implementation of a

Please fix the deque.template file so that the test program runs correctly.

My program:

deque.template

//File: deque.template //This program is a static implementation of a deque

#include #include #include "deque.h"

using namespace std;

//postcondition: empty deque has been created template deque:: deque() { count()=0; first() = NULL; last() = NULL; } // postcondition: all resouroces allocated to the deque // have been deallocated template deque::~deque() { list_clear(first); }

// postcondition: newly created deque is a copy of dq template deque::deque(const deque& dq) { list_copy(dq.first, first, last); count = dq.count; } //precondition: deque is not empty //postcondition: reference to element at front of deque //has been returned template T& deque:: front() { assert(!empty());

return return first->data(); } //precondition: deque is not empty //postcondition: copy of element at front of deque //has been returned template T deque:: front() const { assert(!empty()); return first->data(); } //precondition: deque is not empty //postcondition: returns reference to element at front of deque template T& deque:: back() { assert(!empty()); return last->data(); } //precondition: deque is not empty //postcondition: returns copy of element at back of deque template T deque:: back() const { assert(!empty()); return last->data(); } //postcondition: entry has been inserted at the front of the deque template void deque:: push_front (const T& entry) { list_head_insert(first, entry); if (size() == 1) { last = first; } } //postcondition: entry has been inserted at the back of the deque template void deque:: push_back (const T& entry) { if (empty()) { list_head_insert(first, entry); last = first; } else { list_insert(last, entry); last = last->link(); } } //precondition: deque is not empty //postcondition: element at front of deque has been removed template void deque:: pop_front() { assert(!empty()); if (first == last) { last = NULL; } list_head_remove(first); } //precondition: deque is not empty //postcondition: element at back of deque has been removed template void deque:: pop_back() { assert(!empty()); node* tmp = first; if (first == last) { last = NULL; list_head_remove(first); } else { while (tmp->link() != last && tmp != NULL) { tmp = tmp->link(); } last = tmp; list_remove(tmp); }

} //postcondition: number of elements in deque has been returned template size_t deque:: size() const { return list_length(first); } //postcondition: returns whether deque is empty template bool empty() const { return size()==0; } //postcondition: returns whether 2 deques are equal - equal is defined //as the deques have the same number of elements & //corresponding elements are equal template bool operator == (const deque& dq1, const deque& dq2) { if (dq1.size() != dq2.size()) return false; const_node_iterator j(dq2.first); for (const_node_iterator i(dq1.first); i != NULL; i++) { if (*i != *j) return false; j++; } return true; } //postcondition: dq has been display from front to rear on out template std::ostream& operator<< (std::ostream& out, const deque& dq) { }

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

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; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions