数据结构:十字链表实现矩阵相加

任务

应用十字链表存储稀疏矩阵元素,并实现矩阵相加

所需

  • C语言运行平台
  • 矩阵运算知识
  1. 发现指针若未初始化,不为空,但访问时出错
    解决:初始化时赋空
 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;
  1. 若在已有位置插入元素,采用相加模式
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;
  }
 }
}

结果展示

在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值