一组排序矩阵相关操作,数据结构使用的是多维链表

typedef struct stMatrixNode
{
 Item data;
 unsigned coloum;
 stMatrixNode *next;
}MatrixNode, *MatrixLink;

 

//多维表矩阵的基本操作及乘法运算
MatrixLink NewMatrixNode(unsigned coloum, Item data)
{
 MatrixLink node = (MatrixLink)malloc(sizeof(MatrixNode));
 if (!node)
 {
  return NULL;
 }

 node->data = data;
 node->coloum = coloum;
 node->next = NULL;

 return node;
}

MatrixLink* NewMatrixHead(unsigned num)
{
 unsigned i;

 MatrixLink *pMatrix = (MatrixLink*)malloc(sizeof(MatrixLink) * (num+1));
 if (!pMatrix)
 {
  return NULL;
 }
 
 for (i=0; i<=num; i++)
 {
  pMatrix[i] = NULL;
 }

 return pMatrix;
}

MatrixLink* CreateMatrix(unsigned num)
{
 unsigned i;

 MatrixLink *pMatrix = NewMatrixHead(num);
 if (!pMatrix)
 {
  return NULL;
 }

 MatrixLink node;
 Item data;
 unsigned coloum;

 MatrixLink curNode;
 MatrixLink nextNode;

 for (i=0; i<num; i++)
 {
  while(scanf("%d %d", &coloum, &data) == 2)
  {
   node = NewMatrixNode(coloum, data);
   if (!node)
   {
    continue;
   }
   if (!(pMatrix[i]) || (pMatrix[i]->coloum>=node->coloum))
   {
    if (pMatrix[i]->coloum == node->coloum)
    {
     pMatrix[i]->data = node->data;
    }
    else
    {
     node->next = pMatrix[i];
     pMatrix[i] = node;
    }
   }
   else
   {
    curNode = pMatrix[i];
    while(curNode->next)
    {
     nextNode = curNode->next;
     if (nextNode->coloum > node->coloum)
     {
      node->next = nextNode;
      curNode->next = node;
      break;
     }
     if (nextNode->coloum == node->coloum)
     {
      nextNode->data = node->data;
      break;
     }
     curNode = curNode->next;
    }

    if (!nextNode)
    {
     node->next = nextNode;
     curNode->next = node;
    }
   }
  }
 }

 return pMatrix;
}

MatrixLink* MultiMatrix(MatrixLink *mtx1, MatrixLink *mtx2)
{
 MatrixLink node1,node2,curNewNode,nextNewNode,newNode;
 MatrixLink *pNewMatrix;
 int i;

 int rowNum=0;
 for (i=0; mtx1[i]!=NULL; i++)
 {
  rowNum++;
 }

 pNewMatrix = NewMatrixHead(rowNum-1);
 if (!pNewMatrix)
 {
  return NULL;
 }

 for (i=0; mtx1[i]!=NULL; i++)
 {
  node1 = mtx1[i];
  while(node1)
  {
   node2 = mtx2[node1->coloum-1];
   while(node2)
   {
    if ((!pNewMatrix[i]) || (pNewMatrix[i]->coloum >= node2->coloum))
    {
     if (pNewMatrix[i]->coloum == node2->coloum)
     {
      pNewMatrix[i]->data += node1->data*node2->data;
     }
     else
     {
      newNode = (MatrixLink)malloc(sizeof(MatrixNode));
      while(!newNode)
      {
       newNode = (MatrixLink)malloc(sizeof(MatrixNode));
      }

      newNode->data = node1->data + node2->data;
      newNode->coloum = node2->coloum;

      newNode->next = pNewMatrix[i]->next;
      pNewMatrix[i] = newNode;
     }
    }
    else
    {
     curNewNode = pNewMatrix[i];

     while(curNewNode->next)
     {
      nextNewNode = curNewNode->next;

      if (nextNewNode->coloum > node2->coloum)
      {
       newNode = (MatrixLink)malloc(sizeof(MatrixNode));
       while(!newNode)
       {
        newNode = (MatrixLink)malloc(sizeof(MatrixNode));
       }

       newNode->data = node1->data + node2->data;
       newNode->coloum = node2->coloum;

       newNode->next = nextNewNode;
       curNewNode->next = newNode;
       break;
      }
      
      if (nextNewNode->coloum == node2->coloum)
      {
       nextNewNode->data += node1->data*node2->data;
       break;
      }

      curNewNode = curNewNode->next;
     }

     if (!nextNewNode)
     {
      newNode = (MatrixLink)malloc(sizeof(MatrixNode));
      while(!newNode)
      {
       newNode = (MatrixLink)malloc(sizeof(MatrixNode));
      }
      
      newNode->data = node1->data + node2->data;
      newNode->coloum = node2->coloum;
      
      newNode->next = nextNewNode;
      curNewNode->next = newNode;
     }
    }

    node2 = node2->next;
   }

   node1 = node1->next;
  }
 }

 return pNewMatrix;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值