Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement deque class using Arrays : deque.h provided below deque.template was n

ID: 3803908 • Letter: I

Question

Implement deque class using Arrays:

deque.h provided below
deque.template was not provided

deque.h

#ifndef _DEQUE_H_
#define _DEQUE_H_

#include <iostream>
#include <cstdlib>

template <typename T>
class deque
{
public:
typedef std::size_t size_type;
static const size_type CAPACITY = 10;
  
//postcondition: empty deque has been created
deque();
  
//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;
  
// precondition: deque is not full
// postcondition: entry has been inserted at the front
// of the deque
void push_front (const T& entry);
  
// precondition: deque is not full
// 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: whether deque is full has been returned
bool full() 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:
T data[CAPACITY]; // Circular array
size_type first; // Index of item at front of the queue
size_type last; // Index of item at rear of the queue
size_type count; // Total number of items in the queue

// postcondition: returned next index in array
size_type next_index(size_type i) const;
  
// postcondition: returned previous index in array
size_type prev_index (size_type i) const;
};

#include "deque.template"

#endif

Explanation / Answer

Answer:

#include <iostream.h>

using namespace std;
#define SIZE 5

class dequeue
{

int input[10],end_front,end_rear,incrementer;

public:
dequeue();
void include_first(int);
void include_second(int);
void remove_front();
void remove_rear_end();
void show();
};


dequeue::dequeue()
{
end_front=-1;
end_rear=-1;
incrementer=0;
}


void dequeue::include_first(int value)
{
if(end_front==-1)
{
end_front++;
end_rear++;
input[end_rear]=value;
incrementer++;
}
else if(end_rear>=SIZE-1)
{
cout<<" Insertion is not possible,overflow!!!!";
}
else
{
for(int i=incrementer;i>=0;i--)
{
input[i]=input[i-1];
}
input[i]=value;
incrementer++;
end_rear++;
}
}

void dequeue::include_second(int value)
{

if(end_front==-1)
{
end_front++;
end_rear++;
input[end_rear]=value;
incrementer++;
}
else if(end_rear>=SIZE-1)
{
cout<<" Insertion is not possible,overflow!!!";
return;
}
else
{
input[++end_rear]=value;
}


}

void dequeue::show()
{

for(int i=end_front;i<=end_rear;i++)
{
cout<   }
}


void dequeue::remove_front()
{
if(end_front==-1)
{
cout<<"Deletion is not possible:: Dequeue is empty";
return;
}
else
{
if(end_front==end_rear)
{
end_front=end_rear=-1;
return;
}
cout<<"The deleted element is "<
end_front=end_front+1;
}


}

void dequeue::remove_rear_end()
{
if(end_front==-1)
{
cout<<"Deletion is not possible:Dequeue is empty";
return;
}
else
{
if(end_front==end_rear)
{
end_front=end_rear=-1;
}
cout<<"The deleted element is "<< input[end_rear];
end_rear=end_rear-1;
}


}

void main()
{
int c,value;
dequeue d1;
clrscr();
do
{
cout<<" ****DEQUEUE OPERATION**** ";
cout<<" 1-Insert at beginning";
cout<<" 2-Insert at end";
cout<<" 3_show";
cout<<" 4_Deletion from end_front";
cout<<" 5-Deletion from end_rear";
cout<<" 6_Exit";
cout<<" Enter your choice<1-4>:";
cin>>c;

switch(c)
{
case 1:
cout<<"Enter the element to be inserted:";
cin>>value;
d1.include_first(value);
break;

case 2:
cout<<"Enter the element to be inserted:";
cin>>value;
d1.include_second(value);
break;

case 3:
d1.show();
break;

case 4:
d1.remove_front();
break;
case 5:
d1.remove_rear_end();
break;

case 6:
exit(1);
break;

default:
cout<<"Invalid choice";
break;
}

}while(c!=7);
getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote