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