链表重载等号_c++ - 四重链表矩阵的重载运算符[] - 堆栈内存溢出

我创建了一个四链链表,以在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<

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

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

vector> elements;

};

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值