最近在复习数据结构,顺便看看大一的时候写的代码,看完之后比当初有了更加深刻的体会。
希望这些能提供给初学者一些参考。
//鏈式存儲的數據結構
typedef struct node
{
char data;
struct node *next;
}strLink;
// 塊鏈
#define NodeNum 節點中存放的字節數
typedef struct node
{
char data[NodeNum];
struct node *next;
}BlockLink;
//模式匹配的Brute-Force
int Index (SeqString s1, SeqString s2)
{
int i=0, j=0;
while (i < s1.StrLength && j < s2.StrLength)
if (s1.char[i] == s2.char[j])
{
i++;
j++;
}
else
{
i -= j;
j = 0;
}
if (j >= s2.StrLength)
return (i-s2.StrLength);
return (-1);
}
//三元組的數據類型
#define MaxSize 非零元素的最大個數
typedef struct
{
int r, c;
ElemType d;
}Triple;
typedef struct
{
int row, col, num;
Triple data[MaxSize];
}TSMatrix
//創建矩陣的三元組
/*以行列的方式掃描具有n行m列的二維數組a,
將其非 0 元素存放在 t 所指的三原表中*/
void Create (TSMatrix *t, ElemType a[], int m, int n)
{
t->row = m;
t->col = n;
t->num = 0;
for (i=0; i < m; i++)
for (j=0; j < n; j++)
if (a[i*n+j] != 0)
{
t->data[t->num].r = i;
t->data[t->num].c = j;
t->data[t->num].d = a[i*n+j];
t->num++;
}
}