任务
应用十字链表存储稀疏矩阵元素,并实现矩阵相加
所需
- C语言运行平台
- 矩阵运算知识
- 发现指针若未初始化,不为空,但访问时出错
解决:初始化时赋空
for (pc = m.column; pc - m.column < m.nu; pc++)
pc->first = NULL;
for (pr = m.row; pr - m.row < m.mu; pr++)
pr->first = NULL;
- 若在已有位置插入元素,采用相加模式
if (j == current->j)
{
current->data += e;
free(n);
insert = true;
break;
}
列不做处理(因为已有结点)
if (i == current->i)
{
insert = true;
break;
}
代码实现
数据结构代码表示
#define ERROR 0
#define OK 1
typedef int status;
十字链表结构
typedef int MElemType;
typedef struct crossNode {
int i, j;
MElemType data;
struct crossNode* down;
struct crossNode* right;
}*crossList;
typedef struct Matric {
int mu, nu, tu;
struct Head* row;
struct Head* column;
};
typedef struct Head {
struct crossNode* first;
}*HList;
初始化矩阵
status initMatric(Matric& m, int r, int c)
{
HList pc, pr;
m.mu = r;
m.nu = c;
m.tu = 0;
m.row = (HList)malloc(sizeof(Head) * m.mu);
m.column = (HList)malloc(sizeof(Head) * m.nu);
for (pc = m.column; pc - m.column < m.nu; pc++)
pc->first = NULL;
for (pr = m.row; pr - m.row < m.mu; pr++)
pr->first = NULL;
if (m.row == NULL || m.column == NULL)
return ERROR;
else
return OK;
}
插入新元素至矩阵
status addElem(Matric& m, int i, int j, MElemType e)
{
crossNode* n;
crossList current,last=NULL;
HList pr, pc;
bool insert=false;
pr = m.row+i;//对应行
pc = m.column+j;//对应列
n = (crossNode*)malloc(sizeof(crossNode));
if (n == NULL)
return ERROR;
n->data = e;
n->i = i;
n->j = j;
if (pr->first == NULL)
{
pr->first = n;
n->right = NULL;
}
else
{
current = pr->first;
while (current != NULL)
{
if (j < current->j)
{
n->right = current->right;
current->right = n;
n->data = current->data;
current->data = e;
insert = true;
break;
}
if (j == current->j)
{
current->data += e;
free(n);
insert = true;
break;
}
last = current;
current = current->right;
}
if (insert == false)
{
last->right = n;
n->right = NULL;
}
}
insert = false;
if (pc->first == NULL)
{
pc->first = n;
n->down = NULL;
}
else
{
current = pc->first;
while (current != NULL)
{
if (i < current->i)
{
n->down = current->down;
current->down = n;
n->data = current->data;
current->data = e;
insert = true;
break;
}
if (i == current->i)
{
insert = true;
break;
}
last = current;
current = current->down;
}
if (insert == false)
{
last->down = n;
n->down = NULL;
}
}
return OK;
}
矩阵相加
status addMatrics(Matric&m1, Matric m2)
{
HList pr;
crossList p;
pr = m2.row;
for (pr; pr - m2.row < m2.mu; pr++)
{
if (pr->first != NULL)
{
p = pr->first;
while (p != NULL)
{
addElem(m1, p->i, p->j, p->data);
p = p->right;
}
cout << endl;
}
}
return OK;
}
分别按行优先、列优先显示矩阵
void showMatric(Matric m)
{
HList pr;
crossList p;
cout << "in row:" << endl;
pr = m.row;
for (pr; pr - m.row < m.mu; pr++)
{
if (pr->first != NULL)
{
p = pr->first;
cout << "row " << p->i << endl;
while (p != NULL)
{
cout << p->i << " " << p->j << " " << p->data << endl;
p = p->right;
}
cout << endl;
}
}
cout << "in column:" << endl;
pr = m.column;
for (pr; pr - m.column < m.nu; pr++)
{
if (pr->first != NULL)
{
p = pr->first;
cout << "col " << p->j << endl;
while (p != NULL)
{
cout << p->i << " " << p->j << " " << p->data << endl;
p = p->down;
}
cout << endl;
}
}
}