The purpose of this lab is to help reinforce linear data structure implementatio
ID: 3734684 • Letter: T
Question
The purpose of this lab is to help reinforce linear data structure implementations in C++. Specifically, the lab is to construct a C++ implementation of a static deque class. Use the “deque.h”.
------deque.h---------
#ifndef _DEQUE_H_
#define _DEQUE_H_
#include <iostream>
#include <cstdlib>
#include "node2.h"
using namespace main_savitch_6B;
template <typename T>
class deque
{
public:
typedef std::size_t size_type;
//postcondition: empty deque has been created
deque();
// postcondition: all resouroces allocated to the deque
// have been deallocated
~deque();
// postcondition: newly created deque is a copy of dq
deque(const deque<T>& dq);
// postcondition: current deque is a copy of dq
deque<T>& operator = (const deque<T>& dq);
//precondition: deque is not empty
// postcondition: reference to element at front of deque
// has been returned
T& front();
// precondition: deque is not empty
// postcondition: copy of element at front of deque
// has been returned
T front() const;
// precondition: deque is not empty
// postcondition: reference to element at front of deque
// has been returned
T& back();
// precondition: deque is not empty
// postcondition: copy of element at back of deque
// has been returned
T back() const;
// postcondition: entry has been inserted at the front
// of the deque
void push_front (const T& entry);
// postcondition: entry has been inserted at the back
// of the deque
void push_back (const T& entry);
// precondition: deque is not empty
// postcondition: element at front of deque has been removed
void pop_front();
// precondition: deque is not empty
// postcondition: element at back of deque has been removed
void pop_back();
// postcondition: number of elements in deque has been returned
size_type size() const;
// postcondition: whether deque is empty has been returned
bool empty() const;
// postcondition: returned whether 2 deques are equal - equal is defined
// as the deques have the same number of elements &
// corresponding elements are equal
template <typename U>
friend bool operator == (const deque<U>& dq1, const deque<U>& dq2);
// postcondition: dq has been display from front to rear on out
template <typename U>
friend std::ostream& operator<< (std::ostream& out, const deque<U>& dq);
private:
size_type count; // Total number of items in the queue
node<T>* first;
node<T>* last;
};
#include "deque.template"
#endif
Explanation / Answer
#include<deque.h>
class StaticDeque
{
public:
deque dq;
int start,end;
StaticDeque()
{
dq = deque();
dq.size = 50 // size of deque is 50
start =0,end=0;
}
void insertAtFront(int val)
{
if((end-start)<dq.size)
{
dq.push_front(val);
start--;
}
else
{
// can't insert static deque is full
}
}
void insertAtBack(int val)
{
if((end-start)<dq.size)
{
dq.push_back(val);
end++;
}
else
{
// can't insert static deque is full
}
}
void removeFromFront()
{
if(start<0)
{
dq.pop_front();
start++;
}
else
{
// No element to pop
}
}
void removeFromBack()
{
if(end>0)
{
dq.pop_back();
end--;
}
else
{
// No element to pop;
}
}
int sizeOfDeque()
{
return (end-start);
}
bool isEmpty()
{
return dq.empty()
}
bool areEqual(const StaticDeque &dq1)
{
return dq1.dq == dq
}
void print()
{
cout<<dq;
}
};
int main()
{
StaticDeque obj;
obj = StaticDeque();
// add or remove element in obj of class staticdeque
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.