0

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.

Keagansed
  • 183
  • 1
  • 1
  • 13
  • 1
    There is no `operator[][]` to overload actually. – πάντα ῥεῖ Sep 29 '16 at 16:43
  • @NathanOliver this is not a duplicate question as this is not an array of arrays and cannot simply refer to another object using aggregation. I need a solution to my specific problem – Keagansed Sep 29 '16 at 16:56
  • 1
    Why would you want to use `[][]` on a linked-list instance? It will be `O(n)` in time-complexity, as a user I would expect `[]` accessors to be `O(log n)` at most (i.e. in the case of maps) - providing a linear in size subscript accessor is not good design in my opinion. – sjrowlinson Sep 29 '16 at 17:34
  • @ArchbishopOfBanterbury I just wanted to make accessing elements as easy as possible. How else would I access the elements? – Keagansed Sep 29 '16 at 17:38
  • 1
    The typically c++ way to do it is via exposing an iterator interface for your data structures. Also, you should consider whether you actually need a linked-list structure if you are going to be doing lots of element access - consider using `std::vector` instead. – sjrowlinson Sep 29 '16 at 17:50
  • Unfortunately it's an assignment so I have to use a linked list but I will use an iterator interface to access elements. Thank you. – Keagansed Sep 29 '16 at 17:57

0 Answers0