数据结构与算法基础之对称矩阵
学习来源:
数据结构与算法基础(青岛大学-王卓)
地址:
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)