c++) using the following code, modify to test these objectives:
- In the main(), include test cases for Deque and Deque, respectively. To be specific, test each operation listed above in the Deque class at least once for each Deque object.
- For each test case in your code, print the name of the test case on the console and call display() as a verification. No points will be given to a test case that does not display the name of the test case and the result of the test case.
- When you fill each Deque with many keys or elements, use a loop and a random number generator if you can.
- Do not interact with the keyboard in the test code so that program testing time can be reduced.
- Catch all exceptions that can be raised by the Deque.
#include #include
using namespace std;
template class Node { public: T data; Node *prev; Node *next; public: Node(T item) { data = item; prev = NULL; next = NULL; } }; template class Deque { private: int count; Node* head; Node* tail;
public: Deque(); Deque(const Deque &); Deque& operator=(const Deque &); ~Deque(); int size() const; bool isEmpty() const; void display() const; void removeHead(T &x); void removeTail(T &x); void insertHead(T &x); void insertTail(T &x); }; template Deque :: ~Deque() { Node *temp = head; while (head != 0) { head = head->next; delete temp; temp = head; } tail = NULL; } template Deque ::Deque(const Deque &l1) { Node *p = l1.head; head = tail = 0; while (p != NULL) { this->addToTail(p->data); p = p->next; } } template Deque& Deque :: operator=(const Deque &l1) { Node *p = l1.head; head = tail = 0; while (p != NULL) { this->addToTail(p->data); p = p->next; } return(*this); } template int Deque::size() const { int count = 0; Node *c = head; while (c != 0) { c = c->next; count++; } return count; } template bool Deque::isEmpty() const { return head == 0; } template Deque::Deque() { count = 0; head = 0; tail = 0; } template void Deque::insertHead(T &d) { Node *p = new Node(d); if (head == NULL) { head = p; tail = p; } else { head->prev = p; p->next = head; head = p; } } template void Deque::removeTail(T &d) { try { if (isEmpty()) { throw "List is Empty"; } } catch (const char *s) { cout << s; } T temp; if (head == tail) { delete head; head = tail = 0; } else { temp = tail->data; tail = tail->prev; delete tail->next; tail->next = 0; } cout << " " << temp << " Deleted Successfully. "; } template void Deque::removeHead(T &d) { try { if (isEmpty()) { throw "List is Empty"; } } catch (const char *s) { cout << s; } T temp; if (head == tail) { delete head; head = tail = 0; } else { temp = head->data; head = head->next; delete head->prev; head->prev = 0; } cout << " " << temp << " Deleted Successfully. "; } template void Deque::insertTail(T &d) { Node *p = new Node(d); if (head == NULL) { head = p; tail = p; } else { tail->next = p; p->prev = tail; tail = p; } } template void Deque::display() const { Node *p = head; try { if (head == NULL) throw "xxxxx List is Empty xxxxx"; else { cout << "ELEMENTS :-> "; while (p != NULL) { cout << p->data << " "; p = p->next; } } } catch (const char *s) { cout << s; }
} int main() { Deque obj; int x = 3, y = 4; cout << "Adding Element to head: "; obj.insertHead(x); obj.display(); cout << " Adding Element to head: "; obj.insertHead(y); obj.display(); cout << " Adding Element to Tail: "; obj.insertTail(y); obj.display(); cout << " Intial List : "; obj.display(); cout << " Removing Element from head: "; obj.removeHead(x); obj.display(); cout << " Removing Element from tail: "; obj.removeTail(y); obj.display(); cout << " List Size: "; cout << obj.size(); }