数组实验代码
#include <stdio.h>
#include <iostream>
#define ERROR -1
#define MAXSIZE 12500 //非零元个数最大值MAXSIZE
#define MAXRC 21 //各行第一个非零元位置最大值MAXRC
#define OK 1
typedef int ElemType;
typedef struct
{
int i,j;
ElemType e;
} Triple;
typedef struct
{
Triple data[MAXSIZE+1]; //非零元三元组表
int rpos[MAXRC+1]; //各行第一个非零元的位置表
int mu,nu,tu; //矩阵的行数、列数和非零元个数
} RLSMatrix;
//创建
void CreatSMatrix(RLSMatrix &M) //建立以“带行链接信息”的三元组顺序表示的稀疏矩阵
{
for(int i=1; i<=MAXRC+1; i++)
M.rpos[i]=0; //令所有的位置都为0
printf("请输入矩阵的行数、列数和非零元个数(以空格隔开):");
scanf("%d %d %d",&M.mu,&M.nu,&M.tu);
for(int i=1; i<=M.tu; i++)
{
printf("请用三元组形式输入矩阵的元素(行 列 非零元素):");
scanf("%d %d %d",&M.data[i].i,&M.data[i].j,&M.data[i].e);
}
for(int i=1,j=1; i<=M.mu; i++) //求M.rpos[i]
{
M.rpos[i]=j;
while(M.data[j].i==i && j<=M.tu) j++; //若某一行的元素多于1个时
}
}
//相加
bool AddSMatrix(RLSMatrix M,RLSMatrix N,RLSMatrix &Q) //矩阵相加
{
if(M.mu!=N.mu||M.nu!=N.nu)return ERROR;
int i,j,k=1;
Q.mu=M.mu;
Q.nu=M.nu;
for(i=1,j=1