我必须为最初都指向空的行和列创建一个指针数组。
新创建的行/列标题的地址将被插入到数组中。
在结果中,我看到地址在行和列数组中重复。
- 问题:地址在行和列数组中重复。
我不得不提到我没有使用 delete (用于释放内存,因为我很困惑在函数内部还是外部将它包含在哪里)。
我还看到没有为令牌 2x3y3 创建行标题。
First I created an array of pointers
//allocating row and column pointers, m is number of rows and n is number of columns
- node **rArr = new node*[m+1];
- node **cArr = new node*[n+1];
void create_array_with_nullp(){
for(int i=0; i<=m;i++){
rArr[i]=NULL;
std::cout<<"row array contents"<<rArr[i]<<'\n';
}
for(int i=0; i<=n;i++){
cArr[i]=NULL;
std::cout<<"col array contents"<<cArr[i]<<'\n';
}
}
然后,如果尚未通过以下函数创建令牌,我将为令牌创建行头或列头:
void create_n_link_new_node(int a, int b){
if(a >m || b>n || a<0 || b<0){
return;
}
node * colptr = cArr[b];
node * rowptr = rArr[a];
if (rowptr==NULL){
node * new_rowheader = new node;
new_rowheader->coefficient = NULL;
new_rowheader->row = a;
new_rowheader->column = -1;
new_rowheader->rowLink = new_rowheader;
new_rowheader->colLink = new_rowheader;
rArr[a] = new_rowheader;
std::cout<<"new row header created"<<'\n';
std::cout<< "coefficient = "<<new_rowheader->coefficient<<'\n';
std::cout<< "row = "<<new_rowheader->row<<'\n';
std::cout<< "column = "<<new_rowheader->column<<'\n';
std::cout<< "rowLink = "<<new_rowheader->rowLink<<'\n';
std::cout<< "colLink = "<<new_rowheader->colLink<<'\n';
}
if(colptr == NULL){
node * new_colheader = new node;
new_colheader->coefficient = NULL;
new_colheader->row = -1;
new_colheader->column = b;
new_colheader->rowLink = new_colheader;
new_colheader->colLink = new_colheader;
cArr[b] = new_colheader;
std::cout<<"new column header created"<<'\n';
std::cout<< "coefficient = "<<new_colheader->coefficient<<'\n';
std::cout<< "row = "<<new_colheader->row<<'\n';
std::cout<< "column = "<<new_colheader->column<<'\n';
std::cout<< "rowLink = "<<new_colheader->rowLink<<'\n';
std::cout<< "colLink = "<<new_colheader->colLink<<'\n';
}
}
结果是:
THE RESULT:
token==5x4y2
coefficient=5
row= 4
col = 2
new row header created
coefficient = 0
row = 4
column = -1
rowLink = 0x100103c10
colLink = 0x100103c10
new column header created
coefficient = 0
row = -1
column = 2
rowLink = 0x100103c30
colLink = 0x100103c30
token==8x4y
coefficient=8
row= 4
col = 1
new column header created
coefficient = 0
row = -1
column = 1
rowLink = 0x100103c50
colLink = 0x100103c50
token==2x3y3
coefficient=2
row= 3
col = 3
new column header created
coefficient = 0
row = -1
column = 3
rowLink = 0x100103c70
colLink = 0x100103c70
token==4xy2
coefficient=4
row= 1
col = 2
new row header created
coefficient = 0
row = 1
column = -1
rowLink = 0x100103c90
colLink = 0x100103c90
token==y3
coefficient=-1
row= 0
col = 3
new row header created
coefficient = 0
row = 0
column = -1
rowLink = 0x100103cb0
colLink = 0x100103cb0
token==5y
coefficient=5
row= 0
col = 1
token==5
coefficient=5
row= 0
col = 0
new column header created
coefficient = 0
row = -1
column = 0
rowLink = 0x100103cd0
colLink = 0x100103cd0
为什么没有为 token=2x3y3 创建行头?
最后存储在指针数组中的地址是:
#Array of row pointers#
- row 0 = 0x100103cb0
- row 1 = 0x100103c90
- row 2 = 0x100103cd0
- row 3 = 0x100103c50
- row 4 = 0x100103c30
#Array of column pointers#
- column 0 = 0x100103cd0
- column 1 = 0x100103c50
- column 2 = 0x100103c30
- column 3 = 0x100103c70
* row 2 is having the same address of column 0,
* row 3 is having the same address of column 1,
* row 4 is having the same address of column 2