Question

Template Dequeue Class (C++ )

In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together.

Starter

testDeque.cpp which contains:

  1. Timer class holder (you need to go through the LearnCpp Ch15 and import it in)
  2. Node class
  3. Deque class specification (you need to fill out the definition)

here is the testDeque.cpp code:

// C++ implementation of doubly linked list Deque doubly linked list
#include <bits/stdc++.h>
using namespace std;
class Timer {
// To replace with the full timer class definition
// inside this folder: LearnCpp9_18_timeSortArray.cpp
};
// Node of a doubly linked list
template<class T>
class Node {
public:
T data;
Node<T> *prev, *next;
static Node<T>* getnode(int data) {
Node<T>* n = new Node<T>;
n->data = data;
n->prev = n->next = nullptr;
return n;
}
};
// A doubly linked list deque
template<class T>
class Deque {
Node<T> *head, *tail, *copy;
int size;
// deep copy helper
void deepCopy( const Deque<T> & rhs );
public:
Deque():head(nullptr), tail(nullptr), size(0) { }
Deque( const Deque<T> & rhs ); // copy constructor
Deque( Deque<T> && rhs ); // move constructor
Deque<T> & operator= ( Deque<T> & rhs ); // copy operator=
Deque<T> & operator= ( Deque<T> && rhs ); // move operator=
// Operations on Deque
void pushHead(T data);
void pushTail(T data);
void popHead();
void popTail();
void erase();
T getFront();
T getRear();
int _size() { return size; }
bool isEmpty() { return !head; }
T operator[] (const int &sub);
};
template<class T>
void Deque<T>::deepCopy( const Deque<T> & rhs ) {
Node<T> *newNode = new Node<T>;
Node<T> *current = rhs.head; //current points to the list to be copied
size = rhs.size;//copy the head node
copy = newNode; //create the node
copy->data = current->data; //copy the info
copy->next = current->next; //set the link field of the node to nullptr
copy->prev = current->prev;
tail = head; //make tail point to the head node
current = current->next; //make current point to the next node
//copy the remaining list
while (current != nullptr) {
newNode = new Node<T>; //create a node
newNode->data = current->data; //copy the info
newNode->next = current->next;
newNode->prev = current->prev;
tail->next = newNode;
tail = newNode;
current = current->next;
}
}
// complete the rest of definitions below
// Driver program to test the Link List Deque class
int main() {
Deque<int> dq;
cout << "\nInsert item '5' at tail";
dq.pushTail(5);
cout << "\nInsert item '10' at tail";
dq.pushTail(10);
cout << "\nRear item: " << dq.getRear();
dq.popTail();
cout << "\nAfter deleting tail item, new tail is "
<< dq.getRear();
cout << "\nInserting item '15' in head";
dq.pushHead(15);
cout << "\nFront item: " << dq.getFront()
<< "\nNumber of items in Deque: " << dq._size();
dq.popHead();
cout << "\nAfter deleting head item new head is "
<< dq.getFront();
dq.popHead();
cout << "\nAfter deleting head item new head is "
<< dq.getFront();
cout << "\nInitializing a 10000 item Deque.";
Timer t1;
for(int i=1; i<=10000; i++) dq.pushTail(i);
cout << "\nAdd 1~10000, dq now has "
<< dq._size() << " items.";
double run1 = t1.elapsed();
cout << "\nTime elipsed: " << run1 << " seconds";
Timer t2;
cout << "\nDeep Copy construct dq2";
Deque<int> dq2( dq );
double run2 = t2.elapsed();
cout << "\nTime elipsed: " << run2 << " seconds"
<< "\ndq2 front to rear: " << dq2.getFront()
<< " to " << dq2.getRear();
cout << "\nMove construct dq3";
Timer t3;
Deque<int> dq3(std::move(dq));
double run3 = t3.elapsed();
cout << "\nTime elipsed: " << run3 << " seconds"
<< "\ndq3 front to rear: " << dq3.getFront()
<< " to " << dq3.getRear();
cout << "\ndq2[0] is " << dq2[0]
<< ", dq2[999] is " << dq2[999];
}

 

Make sure you have implemented and validated the Stack/Queue member functions especially the following additional methods for AS 9:

  • subscription operator [ ] read and write (included instarter)
  • subscription over the range exception (included in starter)

Example Test Driver

// Driver program to test the Link List Deque class 
int main() { 
        Deque dq; 
        cout << "\nInsert item '5' at tail"; 
        dq.pushTail(5); 

        cout << "\nInsert item '10' at tail"; 
        dq.pushTail(10); 
        cout << "\nRear item: " << dq.getRear(); 

        dq.popTail(); 
        cout << "\nAfter deleting tail item, new tail is "
             << dq.getRear(); 

        cout << "\nInserting item '15' in head"; 
        dq.pushHead(15); 
        cout << "\nFront item: " << dq.getFront()
                 << "\nNumber of items in Deque: " << dq._size(); 

        dq.popHead(); 
        cout << "\nAfter deleting head item new head is "
                 << dq.getFront();
                 
        dq.popHead(); 
        cout << "\nAfter deleting head item new head is "
                 << dq.getFront();

        cout << "\nInitializing a 10000 item Deque.";
        Timer t1;
        for(int i=1; i<=10000; i++) dq.pushTail(i);
        cout << "\nAdd 1~10000, dq now has " 
                << dq._size() << " items.";
        double run1 = t1.elapsed();
        cout << "\nTime elipsed: " << run1 << " seconds";

        Timer t2;
        cout << "\nDeep Copy construct dq2";
        Deque dq2( dq );
        double run2 = t2.elapsed();
        cout << "\nTime elipsed: " << run2 << " seconds"
                 << "\ndq2 front to rear: " << dq2.getFront() 
                 << " to " << dq2.getRear();
        
        cout << "\nMove construct dq3";
        Timer t3;
        Deque dq3(std::move(dq));
        double run3 = t3.elapsed();
        cout << "\noisy()Time elipsed: " << run3 << " seconds"
                 << "\ndq3 front to rear: " << dq3.getFront() 
                 << " to " << dq3.getRear();

        cout << "\ndq2[0] is " << dq2[0] 
                 << ", dq2[999] is " << dq2[999];
                 
} 

Sample Test Run

Running/home/ubuntu/workspace/comsc200/W Insert item .5 at tail Insert item 10 at tail Rear item: 10 After deleting tail i

Submit

  1. AS10_testDeque.cpp
  2. Deque.h and Timer.h
  3. test run validation
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Deque.h

#include <bits/stdc++.h>

using namespace std;

class Timer {

// Add the code for Timer

// To replace with the full timer class definition

// inside this folder: LearnCpp9_18_timeSortArray.cpp

};

// Node of a doubly linked list

template<class T>

class Node {

public:

               T data;

               Node<T> *prev, *next;

               static Node<T>* getnode(int data) {

               Node<T>* n = new Node<T>;

               n->data = data;

               n->prev = n->next = nullptr;

               return n;

               }

};

// A doubly linked list deque

template<class T>

class Deque {

               Node<T> *head, *tail, *copy;

               int size;

               // deep copy helper

               void deepCopy( const Deque<T> & rhs );

public:

               Deque():head(nullptr), tail(nullptr), size(0) { }

               Deque( const Deque<T> & rhs ); // copy constructor

               Deque( Deque<T> && rhs ); // move constructor

               Deque<T> & operator= ( Deque<T> & rhs ); // copy operator=

               Deque<T> & operator= ( Deque<T> && rhs ); // move operator=

               // Operations on Deque

               void pushHead(T data);

               void pushTail(T data);

               void popHead();

               void popTail();

               void erase();

               T getFront();

               T getRear();

               int _size() { return size; }

               bool isEmpty() { return !head; }

               T operator[] (const int &sub);

               //~Deque(){erase();}

};

template<class T>

void Deque<T>::deepCopy( const Deque<T> & rhs ) {

               Node<T> *newNode = new Node<T>;

               Node<T> *current = rhs.head; //current points to the list to be copied

               size = rhs.size;//copy the head node

               copy = newNode; //create the node

               copy->data = current->data; //copy the info

               copy->next = current->next; //set the link field of the node to nullptr

               copy->prev = current->prev;

               head = copy;

               tail = head; //make tail point to the head node

               current = current->next; //make current point to the next node

               //copy the remaining list

               while (current != nullptr) {

                              newNode = new Node<T>; //create a node

                              newNode->data = current->data; //copy the info

                              newNode->next = current->next;

                              newNode->prev = current->prev;

                              tail->next = newNode;

                              tail = newNode;

                              current = current->next;

               }

}

template <class T>

Deque<T>:: Deque( const Deque<T> & rhs )

{

               head = nullptr;

               tail = nullptr;

               size = 0;

               deepCopy(rhs);

}

template <class T>

Deque<T>::Deque( Deque<T> && rhs ) : head(nullptr),tail(nullptr), size(0)

{

               head = rhs.head;

               size = rhs.size;

               tail = rhs.tail;

               copy = rhs.copy;

               rhs.head = nullptr;

               rhs.size = 0;

               rhs.tail = nullptr;

               rhs.copy = nullptr;

}

template <class T>

Deque<T>& Deque<T>:: operator= ( Deque<T> & rhs )

{

               if(this != &rhs)

               {

                              erase();

                              head = nullptr;

                              tail = nullptr;

                              size = 0;

                              copy = nullptr;

                              deepCopy(rhs);

               }

               return *this;

}

template <class T>

Deque<T>& Deque<T>:: operator= ( Deque<T> && rhs )

{

               if(this != &rhs)

               {

                              erase();

                              head = nullptr;

                              tail = nullptr;

                              copy = nullptr;

                              head = rhs.head;

                              tail = rhs.tail;

                              copy = rhs.copy;

                              size = rhs.size;

                              rhs.head = nullptr;

                              rhs.tail = nullptr;

                              rhs.copy = nullptr;

                              rhs.size = 0;

               }

               return *this;

}

// Operations on Deque

template <class T>

void Deque<T>::pushHead(T data)

{

               Node<T> *newNode = new Node<T>;

               newNode->data = data;

               newNode->next = head;

               newNode->prev = nullptr;

               if(head == nullptr)

               {

                              head = newNode;

                              tail = head;

               }else

               {

                              head->prev = newNode;

                              head = newNode;

               }

               size++;

}

template <class T>

void Deque<T>::pushTail(T data)

{

               Node<T> *newNode = new Node<T>;

               newNode->data = data;

               newNode->next = nullptr;

               newNode->prev = tail;

               if(head == nullptr)

               {

                              head = newNode;

                              tail = newNode;

               }else

               {

                              tail->next = newNode;

                              tail = newNode;

               }

               size++;

}

template <class T>

void Deque<T>::popHead()

{

               if(head != nullptr)

               {

                              Node<T> *temp = head;

                              head = head->next;

                              delete(temp);

                              if(head != nullptr)

                                             head->prev= nullptr;

                              else

                                             tail = nullptr;

                              size--;

               }

}

template <class T>

void Deque<T>::popTail()

{

               if(head != nullptr)

               {

                              Node<T> *temp = tail;

                              tail = tail->prev;

                              delete(temp);

                              if(tail != nullptr)

                                             tail->next = nullptr;

                              else

                                             head = nullptr;

                              size--;

               }

}

template <class T>

void Deque<T>::erase()

{

               while(head != nullptr)

               {

                              Node<T> *temp = head;

                              head = head->next;

                              delete(temp);

               }

               tail = nullptr;

               copy = nullptr;

               size = 0;

}

template <class T>

T Deque<T>::getFront()

{

               if(head != nullptr)

               {

                              return head->data;

               }

               return T(NULL);

}

template <class T>

T Deque<T>::getRear()

{

               if(tail != nullptr)

               {

                              return tail->data;

               }

               return T(NULL);

}

template <class T>

T Deque<T>::operator[] (const int &sub)

{

               if(sub >=0 && sub <size)

               {

                              Node<T> *current = head;

                              int pos = 0;

                              while(pos < sub)

                              {

                                             current = current->next;

                                             pos++;

                              }

                              return current->data;

               }

               return T(NULL);

}

//end of Deque.h

Add a comment
Know the answer?
Add Answer to:
Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1"...

    Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1" << endl; dq1.insertFront(42); dq1.insertBack(216); cout << dq1.peekFront() << " - 42" << endl; cout << dq1.peekBack() << " - 216" << endl; cout << dq1.size() << " - 2" << endl; Deque dq2(dq1); Deque dq3; dq3 = dq1; cout << dq1.removeFront() << " - 42" << endl; cout << dq1.removeBack() << " - 216" << endl; cout << dq2.peekFront() << " - 42" <<...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • can someone please fix this error in the stack file at line 18 ( class Stack2:public...

    can someone please fix this error in the stack file at line 18 ( class Stack2:public Stack { ) this is thew error ----> expected class name before '{' token Queue.h --> #include "Stack.hpp" template <typename ITEM> class Queue { private: ITEM item; Stack <ITEM> * s1= new Stack<ITEM>; Stack <ITEM> * s2= new Stack<ITEM>; public: Queue(){}; bool enqueue (ITEM item); bool dequeue (ITEM &item); bool check = true; ~Queue(); }; //this is the class which contains the functions of...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT