//////////////////////////////////////////////////// // deque class written by Dr. Eggen // spring 2000 // CSCI 2320 /////////////////////////////////////////////////// /////////////////////////////////////////////////// // interface /////////////////////////////////////////////////// #include template class deque { private: struct node { T data; struct node *next; struct node *prev; }; node *head; node *tail; public: deque(); ~deque(); void pushFront(T item); void pushBack(T item); void popFront(); void popBack(); T front(); T back(); bool empty(); display(); }; ////////////////////////////////////////////////////////////// // implementation below ////////////////////////////////////////////////////////////// template bool deque::empty() { if (head == NULL && tail == NULL) return true; return false; } ////////////////////////////////////////////////////////////// template deque::deque() { head = NULL; tail = NULL; } ////////////////////////////////////////////////////////////// template deque::~deque() { node *temp; while (head != NULL) { temp = head; head = head->next; delete temp; } } ////////////////////////////////////////////////////////////// template void deque::pushFront(T item) { node *temp; temp = new node; temp->data = item; temp->next = NULL; temp->prev = NULL; if (head == NULL) { head = temp; tail = temp; } else { head->prev = temp; temp->next = head; head = temp; } } ////////////////////////////////////////////////////////////// template void deque::pushBack(T item) { node *temp; temp = new node; temp->data = item; temp->next = NULL; temp->prev = NULL; if (tail == NULL) head = tail = temp; else { tail->next = temp; temp->prev = tail; tail = temp; } } ////////////////////////////////////////////////////////////// template void deque::popFront() { if (empty()) return; else { if (head == tail) { delete head; head = NULL; tail = NULL; } else { node *temp; temp = head; head = head->next; head->prev = NULL; delete temp; } } } ////////////////////////////////////////////////////////////// template void deque::popBack() { if (empty()) return; else { if (head == tail) { delete tail; tail = NULL; head = NULL; } else { node *temp; temp = tail; tail = tail->prev; tail->next = NULL; delete temp; } } } ////////////////////////////////////////////////////////////// template T deque::front() { return head->data; } ////////////////////////////////////////////////////////////// template T deque::back() { return tail->data; } ////////////////////////////////////////////////////////////// template deque::display() { node *temp; temp = head; while (temp != NULL) { cout << temp->data << endl; temp = temp->next; } cout << endl; }