Question

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

Recommended Steps Step 1. IntQ -> IntDeque 1. Bring up the starter, where the Node is a doubly Linked Node different from the

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 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";
cout << "\nDeep Copy construct dq2";
Timer t2;
Deque<int> dq2( dq );
double run2 = t2.elapsed();
cout << "\nTime elipsed: " << run2 << " seconds"
<< "\ndq2 front to rear: " << dq2.getFront()
<< " to " << dq2.getRear();
cout << "\ndq2[0] is " << dq2[0]
<< ", dq2[1000] is " << dq2[1000];
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 << "\ndq3[0] is " << dq3[0]
<< ", dq3[1000] is " << dq3[1000];
cout << "\nAt this time, dq size is " << dq._size();
}

LearnCpp8_16_timeSortArray.cpp:

#include <iostream>
#include <array>
#include <chrono> // for std::chrono functions
const int g_arrayElements = 10000;
class Timer
{
private:
// Type aliases to make accessing nested type easier
using clock_t = std::chrono::high_resolution_clock;
using second_t = std::chrono::duration<double, std::ratio<1> >;
std::chrono::time_point<clock_t> m_beg;
public:
Timer() : m_beg(clock_t::now()) { }
void reset() { m_beg = clock_t::now(); }
double elapsed() const {
return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
}
};
void sortArray(std::array<int, g_arrayElements> &array) {
// Step through each element of the array
// (except the last one, which will already be sorted by the time we get there)
for (int startIndex = 0; startIndex < g_arrayElements - 1; ++startIndex)
{
// smallestIndex is the index of the smallest element we’ve encountered this iteration
// Start by assuming the smallest element is the first element of this iteration
int smallestIndex = startIndex;
// Then look for a smaller element in the rest of the array
for (int currentIndex = startIndex + 1; currentIndex < g_arrayElements; ++currentIndex)
{
// If we've found an element that is smaller than our previously found smallest
if (array[currentIndex] < array[smallestIndex])
// then keep track of it
smallestIndex = currentIndex;
}
// smallestIndex is now the smallest element in the remaining array
// swap our start element with our smallest element (this sorts it into the correct place)
std::swap(array[startIndex], array[smallestIndex]);
}
}
int main()
{
std::array<int, g_arrayElements> array;
for (int i = 0; i < g_arrayElements; ++i)
array[i] = g_arrayElements - i;
Timer t;
sortArray(array);
std::cout << "Time taken: " << t.elapsed() << " seconds\n";
return 0;
}

Example Test Driver

Github/m10/as10_testDeque_starter.cpp

It is very easy to miss how to invoke a Move Constructor. On line # 132 above,

// <--- this is how to enter the move argument
   Deque dq3(std::move(dq)); // < once dq is moved, it became empty!

the constructor is overloaded with the move argument!

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 it

Submit

  1. AS10_testDeque.cpp
  2. Deque.h and Timer.h
  3. test (with the starter main ) and produce the correct validation
Recommended Steps Step 1. IntQ -> IntDeque 1. Bring up the starter, where the Node is a doubly Linked Node different from the AS9, a singly Linked Node 2. To move and adapt your AS9 IntLinkedQueue to AS10 as IntDeque where the Deque is a Double Ended Queue 3. Comment out all the auxiliary features in the main test driver 4. Add necessary test feature drivers, as you see fit. 5. Test to make sure all features are working correctly Step 2. Template the IntDeque -> Deque 1. Template the Class declaration and definition using proper template syntax 2. Test with two different primitive data types, and make sure all features are working correctly 3. Comment out, or remove the unused features in the main. 4. Test and verify Step 3. Subscript Operator (Lab 10A) 1. Create Overloading Operatorl 2. Test and verify the subscript operator [I directly in the test code (no user interface.) Step 4. Copy Constructor (Module 2) 1. Modify the Copy Constructor for this doubly linked list. 2. You may utilize the provided private helper deepCopy to make a local doubly linked list copy. Alternatively, challenge yourself to write your won! 3. Test and verify Step 5. Timer class (Lab 10B) 1. Add Timer class 2. You may use the provided private helper deepCopy0 is provided for making a duplicated Deque copy 3. Measure the Copy constructor (time) performance for creating a 10000 item Deque. The performance should be similar to the attached test output. Step 6. Move Constructor (Lab 10C) 1. Based on the Lab 10C, construct the Move Constructor per tutorials on the move semantics 2. Measure the Move constructor (time) performance for creating a 10000 item Deque 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) Make sure you have implemented and validated the Stack/Queue member functions especially the following additional methods for AS9 subscription operator [] read and write (included instarter)I/ in assignment 10, your test driver main program shall access the operator[ directly, see the listing below subscription over the range exception (included in starter)
Running/home/ubuntu/workspace/comsc200/w Insert item 5' at tail Insert item '10' at tail Rear item: 10 After deleting tail item, new tail is 5 Inserting item '15' in head Front item: 15 Number of items in Deque: 2 After deleting head item new head is 5 After deleting head item new head is 0 Initializing a 10000 item Deque. Add 1~10000, dq now has 10000 items. Time elipsed: 0.000695639 seconds Deep Copy construct dq2 Time elipsed: 0.000772629 seconds dq2 front to rear: 1 to 10000 Move construct dq3 Time elipsed: q3 front to rear: 1 to dq3 [0] is 1, dq3 [1000] is 10 At this time, dq size is 0 Process exited with code:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//insertion at head

void pushHead(T data)

{

Node *newnode=getnode(data);

if(newnode==NULL)

cout<<" deque overflow";

else

if(head==NULL)

tail=head=newnode;

else

{

newnode->next=head;

head->prev=newnode;

head=newnode;

}

}

//insertion at tail

void pushTail(T data)

{

Node *newnode=getnode(data);

if(newnode==NULL)

cout<<" deque overflow";

else

if(tail==NULL)

head=tail=newnode;

else

{

newnode->prev=tail;

tail->next=newnode;

tail=newnode;

}

}

//deletion from head

void popHead()

{

if(head==NULL)

cout<<"underflow";

else

{

temp=head;

head=head->next;

if(head==NULL)

tail=NULL;

else

head->prev=NULL;

}

//deletion from tail

void popTail()

{

if(tail==NULL)

cout<<"underflow";

else

{

temp=tail;

tail=taill->prev;

if(tail==NULL)

head=NULL;

else

tail->next=NULL;

}

Add a comment
Know the answer?
Add Answer to:
Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...
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 Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    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: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class 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...

  • 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" <<...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • 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;...

  • 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 c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • 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...

  • 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...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

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
ADVERTISEMENT