数据结构与算法基础之对称矩阵(C语言)

数据结构与算法基础之对称矩阵

学习来源:
数据结构与算法基础(青岛大学-王卓)

地址:

https://www.bilibili.com/video/BV1nJ411V7bd?p=22&spm_id_from=pageDriver

背景:
实现视频里老师的伪代码,并不完全一样,但基本雷同。

更新
2021/7/3
第一次发布


代码:

SymmetryMatrix.h

#ifndef _SYMMETRYMATRIX_H_
#define _SYMMETRYMATRIX_H_

typedef struct SymmetryMatrix *SymmetryMatrix;//Matrix object

//Create "n" x "n" symmetry matrix.
SymmetryMatrix Create_SM(unsigned n);

//Destroy matrix.
//The function will set matrix's pointer "sm" with NULL.
void Destroy_SM(SymmetryMatrix *sm_p);

//Set matrix sm's element's value with "val".index("i", "j")
void Set_SM(SymmetryMatrix sm, unsigned i, unsigned j, int val);

//Get matrix sm's element's value.index("i", "j");
int Get_SM(SymmetryMatrix sm, unsigned i, unsigned j);
void Show_SM(SymmetryMatrix sm);

#endif

SymmetryMatrix.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SymmetryMatrix.h"
#include "CommonFunc.h"

struct SymmetryMatrix
{
   
    int *array;//Matrix array.
    unsigned n;//nxn Matrix
};

//Caculate the length of matrix array by "n".
inline static unsigned GetLength_SM(unsigned n);


void Show_SM(SymmetryMatrix sm)
{
   
    NULLPointOut(sm, "Show_SM", "sm");

    unsigned n = sm->n;

    unsigned i, j;
    for(i = 1; i <= n; i++)
    {
   
        for(j = 1; j <=n; j++)
        {
   
            printf("%d ", Get_SM(sm, i, j));
        }
        puts("");
    }

    return;
}

int Get_SM(SymmetryMatrix sm, unsigned i, unsigned j)
{
   
    NULLPointOut(sm, "Get_SM", "sm");

    if(i > sm->n || j > sm->n || i == 0 || j == 0)
    {
   
        puts("ERROR Get_SM: The i or j is out of the matrix boundary of 'n'");
        exit(-1);
    }
    else if(j > i)
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值