I have created a quadruply linked list to create a matrix with variable sized rows in a FlexiMatrix class and would like to oveload the [] operator to allow me to use [][] syntax when trying to print out an element. Unfortunately this is part of an assignment so the class hierarchy can't change. Is it at all possible to achieve this? My classes are as follows:
ListAsSLL:
class ListAsSLL
{
protected:
struct node{
int obj;
struct node* next;
};
node* head;
node* tail;
unsigned listSize;
public:
ListAsSLL()
{
head = 0;
tail = 0;
listSize = 0;
}
virtual ~ListAsSLL()
{
if(head!=0)
{
node* p = head->next;
while (p != 0)
{
p = head->next;
delete head;
head = p;
}
}
}
virtual void addToBeginning(int i)
{
node * temp = new node;
temp->obj = i;
if(head==0){
temp->next = 0;
tail = temp;
}else if(head == tail) {
temp->next = tail;
}
else{
temp->next = head;
}
head = temp;
listSize++;
}
int operator[](int i)
{
if(i >= listSize || i < 0)
{
cout<<"Invalid index provided, try again."<<endl;
} else {
node *ptr = head;
for (int j = 0; j < i; ++j) {
ptr = ptr->next;
}
return ptr->obj;
}
}
};
ListAsDLL:
class ListAsDLL : public ListAsSLL
{
protected:
struct dnode : public node
{
dnode* prev;
};
public:
ListAsDLL() {ListAsSLL();}
virtual ~ListAsDLL()
{
if(head!=0)
{
dnode* p = (dnode*) head->next;
while (p != 0)
{
p = (dnode*) head->next;
delete head;
head = p;
}
}
}
virtual void addToBeginning(int i)
{
dnode * temp = new dnode;
temp->obj = i;
if(head==0){
temp->next = 0;
tail = temp;
}else if(head == tail) {
temp->next = tail;
}
else{
temp->next = head;
}
head = temp;
temp->prev = 0;
listSize++;
}
int operator[](int i)
{
if(i >= listSize || i < 0)
{
cout<<"Invalid index provided, try again."<<endl;
} else {
dnode *ptr = (dnode*) head;
for (int j = 0; j < i; ++j) {
ptr = (dnode*) ptr->next;
}
return ptr->obj;
}
}
};
FlexiMatrix:
class FlexiMatrix : public ListAsDLL
{
public:
FlexiMatrix(unsigned columns)
{
if(columns>0)
{
int count = 0;
this->columns.push_back(columns);
rows = 1;
elements.resize(0);
elements[0].resize(columns);
for (int i = 0; i < columns; ++i)
{
qnode *temp = new qnode;
qnode *pre;
if (i == 0)
{
temp->obj = 1;
temp->prev = 0;
temp->next = 0;
temp->top = 0;
temp->bottom = 0;
head = temp;
tail = temp;
pre = temp;
elements[0][count++] = temp;
}
else
{
temp->obj = 1;
temp->prev = pre;
temp->next = 0;
temp->top = 0;
temp->bottom = 0;
pre->next = temp;
tail = temp;
pre = temp;
elements[0][count++] = temp;
}
}
}
else
{
cout << "Sorry, that is an invalid number of columns." << endl;
}
}
void growColumn(unsigned column, unsigned amount)
{
//....
}
void growRow(unsigned row, unsigned amount)
{
//....
}
ListAsSLL& operator[](int i)
{
//How do I implement this to get [][] funcionality?
}
private:
struct qnode : public dnode
{
qnode* top;
qnode* bottom;
};
unsigned rows;
vector<unsigned> columns;
vector<vector<qnode*>> elements;
};
This is not a duplicate question because I have a specific hierarchy I need to follow and cannot simply refer to another object by aggregation.