I am trying implementing a vector ADT by means of an extandable array used in circular fashion. So now I want to apply the doubling strategy to double the current size of array Wheenever the full exception happens, by using a template to make the vector work
here are my source codes with two header files and three source files, any idea i will be appreciative.
deque.h
**********************************************************
#include "vector.h"
using namespace std;
class deque
{
private:
int qSize;
vector *data;
public:
deque(int size);
~deque();
void insertFirst(int newData);
void insertLast(int newData);
void removeFirst();
void removeLast();
int first();
int last();
int size();
bool isEmpty();
void print();
};
**********************************************************
vector.h
using namespace std;
class vector
{
private:
int *data;
int vSize;
int capacity;
void overflow(); // handle overflow situation
public:
vector(int s);
int elementAtRank(int r);
void insertAtRank(int r, int newData);
void removeAtRank(int r);
void replaceAtRank(int r, int newData);
int size();
bool isEmpty();
~vector();
void print();
};
*****************************************************************
deque.cpp
#include <iostream>
#include "deque.h"
deque::deque(int size)
{
data = new vector(size);
}
deque::~deque()
{
delete data;
}
int deque::size()
{
return data->size();
}
bool deque::isEmpty()
{
return (size()==0);
}
int deque::first()
{
return data->elementAtRank(0);
}
int deque::last()
{
return data->elementAtRank(data->size()-1);
}
void deque::removeFirst()
{
data->removeAtRank(0);
}
void deque::removeLast()
{
data->removeAtRank(data->size()-1);
}
void deque::insertFirst(int newData)
{
data->insertAtRank(0,newData);
}
void deque::insertLast(int newData)
{
data->insertAtRank(data->size(),newData);
}
void deque::print()
{
data->print();
}
***************************************************************
vector.cpp
#include <iostream>
#include "vector.h"
vector::vector(int s)
{
data = new int[s];
capacity=s;
vSize=0;
}
vector::~vector()
{
delete data;
}
int vector::elementAtRank(int r)
{
if(r>=capacity || r<0) // protect data from overflow
return -1;
return data[r];
}
int vector::size()
{
return vSize;
}
bool vector::isEmpty()
{
return (size()==0);
}
void vector::replaceAtRank(int r, int newData)
{
if(r>=capacity || r<0)
return;
data[r]=newData;
}
void vector::removeAtRank(int r)
{
if(r>=capacity || r<0 || isEmpty())
return;
for(int i=r; i<vSize; i++) // shift every element to its left
data[i]=data[i+1];
vSize--;
}
void vector::overflow()
{
capacity*=2; // doubling its size
int *newArray=new int[capacity];
for(int i=0; i<vSize; i++) // copy from old array to the new array
newArray[i]=data[i];
delete data; // delete old data
data = newArray; // old data points to new array
}
void vector::insertAtRank(int r, int newData)
{
if(vSize==capacity) // array is full
overflow();
for(int i=vSize-1; i>=r; i--) // shift every element to its right
data[i+1] = data[i];
data[r] = newData; // insert new data at rank r
vSize++; // size increments
//cout << r << "," << newData << "," << vSize << endl;
}
void vector::print()
{
for(int i=0; i<vSize; i++)
cout << data[i] << ", ";
cout << endl;
}
*******************************************************
main cpp
#include <iostream>
#include "deque.h"
using namespace std;
int main()
{
deque *myQueue = new deque(10);
myQueue->insertFirst(5);
myQueue->insertFirst(7);
myQueue->insertLast(4);
myQueue->insertFirst(3);
myQueue->print();
system("pause");
}