#include "stdio.h"
#define SIZEA 3
#define SIZEB 4
typedef int DataType;
typedef struct node
{
DataType row;
DataType col;
DataType value;
struct node *link;
}Node,*List;
void InitList(List *p)
{
List q;
q =(List)malloc(sizeof(Node));
q->link=NULL;
*p = q;
return;
}
void InsertList(List H,int i,DataType x,DataType row, DataType col)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{ p = p->link;j++;}
q =(List)malloc(sizeof(Node));
q->row=row;
q->col=col;
q->value=x;
q->link=p->link;
p->link=q;
return;
}
void DeleteList(List H,int i)
{ int j=1;
List p,q;
p = H;
while(p->link!=NULL && j<i)
{ p = p->link;j++;}
q = p->link;
p->link=p->link->link;
free(q);
return;
}
int ListLength(List H)
{ int n=0;
List p;
p = H->link;
while(p!=NULL)
{ n++;p=p->link;}
return n;
}
DataType GetList(List H,int i,int *row, int *col)
{
int j=1;
List p;
p = H;
while(p->link!=NULL && j<=i)
{ p = p->link;j++;}
row=p->row;
col=p->col;
return p->value;
}
int EmptyList(List H)
{
if(H->link==NULL)
return 1;
else
return 0;
}
void TravList(List H)
{
List p;
p = H->link;
while(p!=NULL)
{
printf("%d: %d ->%d/t",p->row, p->col,p->value);
p=p->link;
}
printf("/n");
return;
}
void Addition(List L1, List L2, List L3)
{
List p1,p2,p3,p;
int i=1;
p1=L1->link;
p2=L2->link;
p3=L3;
while(p1!=NULL&&p2!=NULL)
{
/*if the exponent is the same, add and put the result to the new list*/
if(p1->row==p2->row && p1->col==p2->col)
{
if(p1->value + p2->value!=0)
InsertList(L3,i,p1->value + p2->value ,p1->row, p1->col) ;
p1=p1->link;
p2=p2->link;
}
/*if not the same, put the higher one into the new list
,and and move the pointer to the next and compare again*/
else if(p1->row < p2->row || (p1->row==p2->row &&p1->col<p2->col))
{
InsertList(L3,i,p1->value,p1->row, p1->col);
p1=p1->link;
}
else if(p1->row>p2->row || (p1->row==p2->row &&p1->col>p2->col))
{
InsertList(L3,i,p2->value,p2->row, p2->col);
p2=p2->link;
}
i++;
p3=p3->link;
}
/*if one list is empty and the other is not, put the rest into the new list*/
if(p1!=NULL)
p3->link=p1;
else if(p2!=NULL)
p3->link=p2;
return;
}
void Mul(List L1, List L2, List L3)
{
List p1, p2, p3,p;
int row, col,i=1,sum=0;
p=L1->link;
p2=L2->link;
p3=L3;
while(p1!=NULL)
{
do
{
p1=p;
while(!(p1->col == p2->row )&& p2->link!=NULL)
{
p2=p2->link;
}
if(p1->col == p2->row )
sum+=p1->value*p2->value;
col=p2->col;
row=p1->row;
/*printf("/np1:(%d:%d)->%d/n", p1->row, p1->col, p1->value);
printf("p2:(%d:%d)->%d/n", p2->row, p2->col, p2->value);
printf("sum is %d/n", sum); */ /*for test*/
p2=p2->link;
p=p1->link;
} while(p1->row==p1->link->row && p1->link!=NULL) ;
if(sum!=0)
{
InsertList(L3,i,sum,row,col) ;
i++;
sum=0;
}
p1=p1->link ;
p2=L2->link ;
/*printf("//p2(%d:%d)->%d///n", p2->row, p2->col, p2->value);*/ /*for test*/
}
return;
}
void PrintMatrix(List M, int size)
{
List p=M->link;
int i,j;
for(i=1;i<=size;i++)
{
for(j=1;j<=size;j++)
{
if(p->row==i && p->col==j && p!=NULL)
{
printf("%d ",p->value);
p=p->link;
}
else
printf("0 ");
}
printf("/n");
}
printf("/n");
return;
}
void main()
{
int row, col,i;
List matrix1, matrix2, matrix3, matrix4;
int m1[]={11,1,22,1,41,1}; /*even index store the position while the odd index store the vlaue */
int m2[]={11,1,21,1,31,1,41,1};
InitList(&matrix1);
InitList(&matrix2);
InitList(&matrix3);
InitList(&matrix4);
for(i=1;i<=SIZEA*2;i+=2) /*Insert the value to the matrix*/
InsertList(matrix1,i, m1[i], m1[i-1]/10,m1[i-1]%10);
for(i=1;i<=SIZEB*2;i+=2)
InsertList(matrix2, i,m2[i], m2[i-1]/10,m2[i-1]%10);
printf("The first matrix is:/n");
PrintMatrix(matrix1,4);
printf("The second matrix is:/n");
PrintMatrix(matrix2,4);;
printf("The addition matrix is:/n");
Addition(matrix1,matrix2,matrix3);
Mul(matrix1,matrix2,matrix4);
PrintMatrix(matrix3,4);
printf("The multify matrix is:/n");
PrintMatrix(matrix4,4);
getch();
return;
}