Question

Deques A deque (pronounced deck) is a double-ended queue. It is a data structure that supports insertion and removal at both

Additional Notes • The calling object should be made constant for any method where the calling objects attribute values shou

Implementation Notes The way I would suggest approaching writing the assignment is something like this: 1. Create a new proje

Simple Test Below is a main function that you can use as a very simple and incomplete test of each of the methods that you ha

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" << endl;
       cout << dq2.peekBack() << " - 216" << endl;
       cout << dq3.peekFront() << " - 42" << endl;
       cout << dq3.peekBack() << " - 216" << endl;
       return 0;
}

C++ Question

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;


struct Node {
int data;
Node *next, *prev;
public:
Node(int val){
data = val;
next=nullptr;
prev=nullptr;
}

friend class Deque;
};


class Deque{
Node *front;
Node *rear;
public:
Deque();
Deque(Deque &copy);
~Deque();
void operator = (Deque &copy);
void insertFront(int val);
void insertBack(int val);
int removeFront();
int removeBack();
int peekFront();
int peekBack();
bool empty();
int size();

};

Deque::Deque(){
front=nullptr;
rear=nullptr;
}

Deque::Deque(Deque &copy){
Node* curr=copy.front;
if(curr==nullptr)
return;
front = new Node(curr->data);
Node *temp = front;
//curr=curr->next;
while(curr->next!=nullptr){
temp->next = new Node(curr->next->data);
temp->next->prev = temp;
curr = curr->next;
temp = temp->next;
}
rear = temp;
}

Deque::~Deque(){
Node *temp=front;
front=nullptr;
Node *temp2;
while(temp!=nullptr){
temp2=temp;
temp=temp->next;
delete(temp2);
}
rear = nullptr;
}

void Deque::operator=(Deque &copy){
//check doubly linked list is same
Node *curr=copy.front;
Node *temp = front;
while(temp!= nullptr && curr!=nullptr)
{
if(temp->data==curr->data){
temp = temp->next;
curr = curr->next;
}else{
break;
}
}
if(temp==nullptr && curr==nullptr){
return;
}
//free the doubly linked list
temp=front;
front=nullptr;
Node *temp2;
while(temp!=nullptr){
temp2=temp;
temp=temp->next;
delete(temp2);
}
rear = nullptr;
//original list is now empty
curr=copy.front;
front = new Node(curr->data);
temp = front;
//curr=curr->next;
while(curr->next!=nullptr){
temp->next = new Node(curr->next->data);
temp->next->prev = temp;
curr = curr->next;
temp = temp->next;
}
rear = temp;

}

void Deque::insertFront(int val){
if(front==nullptr){
front=new Node(val);
rear=front;
return;
}
Node *temp = new Node(val);
temp->next=front;
front->prev=temp;
front=temp;
}

void Deque::insertBack(int val){
if(front==nullptr){
front=new Node(val);
rear=front;
return;
}
Node *temp = new Node(val);
rear->next=temp;
temp->prev = rear;
rear=temp;


}

int Deque::removeFront()
{
if(front==nullptr){
throw "Your list an empty";
}
Node *temp=front;
front=front->next;
int num = temp->data;
if(front!=nullptr)
front->prev=nullptr;
delete(temp);
return num;
}
int Deque::removeBack()
{
if(rear==nullptr){
throw "Your list an empty";
}
Node *temp = rear;
int num = temp->data;
rear = rear->prev;
if(rear!=nullptr)
rear->next=nullptr;
delete(temp);
return num;

}

int Deque::peekFront()
{
if(front==nullptr){
throw "Your list an empty";
}
return front->data;
}

bool Deque::empty()
{
if(front==nullptr)
return true;
return false;
}
int Deque::size()
{
Node *temp=front;
int count=0;
while(temp!=nullptr)
{
count++;
temp=temp->next;
}
return count;

}

int Deque::peekBack()
{
if(front==nullptr){
throw "Your list an empty";
}
return rear->data;

}


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" << endl;
cout << dq2.peekBack() << " - 216" << endl;
cout << dq3.peekFront() << " - 42" << endl;
cout << dq3.peekBack() << " - 216" << endl;
return 0;
}

Output:

1 D:\program\deletebothend.exe 1 1 42 42 216 216 2 - 2 42 42 216 216 42 42 216 216 42 42 216 216 execution time : 0.162 s Pro

Add a comment
Know the answer?
Add Answer to:
Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1"...
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...

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

  • // 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++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

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

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components,...

    Skip list, needs to be implemented in Java: - Implement the class NodeSkipList with two components, namely key node and array of successors. You can also add a constructor, but you do not need to add any method. - In the class SkipList implement a constructor SkipList(long maxNodes). The parameter maxNodes determines the maximal number of nodes that can be added to a skip list. Using this parameter we can determine the maximal height of a node, that is, the...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed...

    PROGRAMMING 1. The LinkList class that we discussed in class is implemented using pointer t constructed with a list of nodes, and the first node pointer points to the front I a) (10 pts) Complete the following class declaration which is similar to Linklist class ecter class (Write only the prototypes and the private data field; definitions will follow the class declaration) template stypenane T> olass Linkedveoter t private olass Node publie T info Node "next typedet Node *nodePtr publie...

  • In this project, you are asked to design and implement a class for double link list...

    In this project, you are asked to design and implement a class for double link list to hold integers: – The class should be called DList, you also need a class called Node that contains an int as the data – Node needs the following data: int data; //the data held by the node Node* parent; //the parent of the Node Node* child; //the child of the Node – Node needs the following public functions: Node(int)// constructor to create a...

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