C语言数据结构学习笔记(8)-稀疏矩阵的三元组顺序表

/*
稀疏矩阵的三元组顺序表基本表示
输出结果为:
请输入稀疏矩阵的行数,列数,非零元素个数:5 5 4
请输入稀疏矩阵的行数,列数,非零元素值:1 2 1
请输入稀疏矩阵的行数,列数,非零元素值:5 3 5
请输入稀疏矩阵的行数,列数,非零元素值:3 2 3
请输入稀疏矩阵的行数,列数,非零元素值:2 4 2
输出三元组数组:
Row     Col     Item
1       2       1
2       4       2
3       2       3
5       3       5
输出稀疏矩阵:
0       1       0       0       0
0       0       0       2       0
0       3       0       0       0
0       0       0       0       0
0       0       5       0       0
稀疏矩阵为5行5列,共4个非零元素.
请按任意键继续. . .
*/
# include <stdio.h>
# include <stdlib.h>
# define MAX 100
# define OK 1
# define ERROR 0
typedef int Status;
typedef struct
{
    int row, col;//三元组的行号,列号
    int item;//三元组的元素值
}Triple;
typedef struct
{
    Triple data[MAX];//非零元素数组
    int mu, nu, num;//稀疏矩阵的行数,列数,非零元素个数
}TSMatrix;

void InitTSMatrix(TSMatrix * pTSM, int m , int n);//初始化稀疏矩阵
void InputTSMatrix(TSMatrix * pTSM, int m , int n, int num);//输入三元组元素
Status SetItem(TSMatrix * pTSM, int row, int col, int item);//添加三元组元素
int GetItem(TSMatrix * pTSM, int row, int col);//根据行,列获取矩阵元素值
void ShowTriple(TSMatrix TSM);//输出三元组数组
void ShowTSMatrix(TSMatrix TSM);//输出稀疏矩阵

int main(void)
{    
    int m , n, num;
    printf("请输入稀疏矩阵的行数,列数,非零元素个数:");
    scanf("%d %d %d", &m, &n, &num);
    TSMatrix TSM;
    InitTSMatrix(&TSM, m, n);
    InputTSMatrix(&TSM, m , n, num);
    ShowTriple(TSM);
    ShowTSMatrix(TSM);
    system("pause");
    return 0;
}
void InitTSMatrix(TSMatrix * pTSM, int m , int n)//初始化稀疏矩阵
{
    pTSM->mu = m;
    pTSM->nu = n;
    pTSM->num = 0;
}
void InputTSMatrix(TSMatrix * pTSM, int m , int n, int num)//输入三元组元素
{
    int row, col, item;
    for(int i = 1; i <= num; i++)
    {    
        printf("请输入稀疏矩阵的行数,列数,非零元素值:");
        scanf("%d %d %d", &row, &col, &item);
        if(item != 0)
        {
            if(ERROR == SetItem(pTSM, row, col, item))
            {
                printf("稀疏矩阵的行数或列数错误或三元组已满.\n");
                break;
            }
        }
    }
}
Status SetItem(TSMatrix * pTSM, int row, int col, int item)//添加三元组元素
{
    if(row > pTSM->mu || col > pTSM->nu)
        return ERROR;
    if(MAX == pTSM->num)
        return ERROR;
    if(0 == item)
        return OK;
    int index = 0;
    while(index < pTSM->num)
    {
        if(row > pTSM->data[index].row)
            index++;
        else if(row == pTSM->data[index].row && col > pTSM->data[index].col)
            index++;
        else
            break;
    }
    if(row == pTSM->data[index].row && col == pTSM->data[index].col)
        pTSM->data[index].item = item;
    else
    {
        for(int i = pTSM->num; i > index; i--)
        {
            pTSM->data[i] = pTSM->data[i-1];
        }
        pTSM->data[index].row = row;
        pTSM->data[index].col = col;
        pTSM->data[index].item = item;
        pTSM->num++;
    }
    return OK;
}
int GetItem(TSMatrix * pTSM, int row, int col)//根据行,列获取矩阵元素值
{
    if(row > pTSM->mu || col > pTSM->nu)
        return 0;
    for(int i = 0; i < pTSM->num; i++)
    {
        if(row == pTSM->data[i].row && col == pTSM->data[i].col)
            return pTSM->data[i].item;
    }
    return 0;
}
void ShowTriple(TSMatrix TSM)//输出三元组数组
{
    printf("输出三元组数组:\n");
    printf("Row\tCol\tItem\n");
    for(int i = 0; i < TSM.num; i++)
        printf("%d\t%d\t%d\n", TSM.data[i].row, TSM.data[i].col, TSM.data[i].item);
}
void ShowTSMatrix(TSMatrix TSM)//输出稀疏矩阵
{
    int index = 0;
    printf("输出稀疏矩阵:\n");
    for(int i = 1; i <= TSM.mu; i++)
    {    
        for(int j = 1; j <= TSM.nu; j++)
        {    
            if(i == TSM.data[index].row && j == TSM.data[index].col)
            {
                printf("%d\t", TSM.data[index].item);
                index++;
            }
            else
                printf("0\t");
        }
        printf("\n");
    }
    printf("稀疏矩阵为%d行%d列,共%d个非零元素.\n", TSM.mu, TSM.nu, TSM.num);
}


版权声明:本文为laven_li原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。