重载operator []用于四重链表的矩阵

时间:2016-09-29 16:42:51

标签: c++ inheritance matrix linked-list operator-overloading

我创建了一个四链表,用于在FlexiMatrix类中创建一个可变大小行的矩阵,并希望过载[]运算符以允许我在尝试打印元素时使用[] []语法。不幸的是,这是作业的一部分,因此类层次结构无法改变。是否有可能实现这一目标?我的课程如下:

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;
};

这不是一个重复的问题,因为我需要遵循一个特定的层次结构,不能简单地通过聚合引用另一个对象。

0 个答案:

没有答案