So two things first off I am taking a data structures class so you cannot use the Standard Library at all none of the standard deque templates this is a purely array based program. Second thing is I do not want you to do my homework for me I am genuinely trying to understand why this isn't working. All of that being said I have it somewhat working on a FIFO basis it works just fine. but on a LIFO basis everything shifts improperly by exactly one. That and it crashes because I haven't set up the list function yet but I will get to that in a little bit. Here is my code. Here is the .h and .cpp files
#ifndef DEQUEUE_H
#define DEQUEUE_H
#include <string>
class dequeue
{
public:
//initialize array and other variables
int* Array;
int numElements;
int ArraySize;
int Left;
int Right;
int value;
static const int SIZE=100;
//dequeue constructor initializes one constructor to 100 elements
//initializes the other to size. also DESTRUCTOR.
dequeue();
dequeue(int n);
virtual ~dequeue();
//adds values to the left and right side of dequeue
void addLeft(int value);
void addRight(int value);
//sets array to zero to see where items are added
void zeroOut();
//removes and returns the left and right most elements
int getLeft();
int getRight();
//returns if the array is full or empty
bool isEmpty();
bool isFull();
//prints content of the array from ltr and rtl
std::string listRightLeft();
std::string listLeftRight();
//debug method that dumps the array from 0 to size -1
std::string dumpArray();
protected:
private:
};
#endif // DEQUEUE_H
#include "dequeue.h"
dequeue::dequeue()
{
Array= new int [SIZE];
ArraySize=SIZE;
numElements=0;
Left=Right=0;
}
dequeue::dequeue(int n)
{
Array= new int [n];
ArraySize=n;
numElements=0;
Left=Right=0;
}
dequeue::~dequeue()
{
delete[] Array;
}
void dequeue::addLeft(int value){
First, I don't recommend this kind queue;
Second, you should modify numElements when adding element;
Third, in function addLeft and addRight, I don't understand the judgement of left, right and ArraySize. Try addRight several times, you will see the problem.
I think the key problem is two if statements in addLeft and addRight.