Special content linear table - multi-dimensional arrays and strings 01

Data structure such as a linear table, stacks and queues, are a linear structure, wherein the data structure is non-atomic element types. Array discussed in this chapter may be considered as an extension of the linear form, i.e., elements in the table is itself a data structure.

Chapter and a brief discussion of the logical structure of the storage array.

Concept array
[] array definition
array (the Array) is a set consisting of the same type of data elements, each data element referred to as an array element (the element).
Long is a one-dimensional array of the table set. It is a two-dimensional array of long table set, each element of which is a one-dimensional array.
n-dimensional array is a linear list, each element of which is the n-1 dimensional array.
Array features:
(1) Promotion of elements: the element itself may have a certain structure, rather than defining a single data element.
(2) the identity element: an element having the same data type.
Uncertainty (3) Relationship: each element are subject to n (n> = 1) constraint linear relationship, such as a two-dimensional array of elements has two subscripts, the elements of the n-dimensional array has n index. The relationship between the number of elements and element generally does not fluctuate.
Storage structure array
storage structure computer is one-dimensional, rather multi-dimensional arrays generally, how to store?
Discussion: The data "into the memory available to obtain the" storage principle, need to find a correspondence relationship with the memory address marked array. Since the structure of the computer memory is a one-dimensional linear, and therefore when storing multidimensional array, the need to address issues related to the multi-dimensional mapping relationship between a dimension, i.e. according to the order of array elements arranged in a certain sequence, the linear sequence is then stored in the memory in.

Array structural characteristics:
the number of a fixed
number of (1) data elements is fixed, once the definition of an array structure, there is no longer increase or decrease in the number of elements.
The same type
(2) data elements of the same type.
Fixed relationship
subscript relationship (3) the data element of the binding and the subscript bounds ordered.
Array basic operations:
(1) the group given a subscript, accessing corresponding data element.
(2) given a set index, modifying a data item value of the corresponding data elements. ~~

1. Multidimensional Arrays

From the data structure point of view, the array is n (n> = 0) a finite sequence of data elements constituting the same data type, the array can be seen as a special kind of linear form, it is to promote the linear form.

Determine characteristics of the array which is suitable for sequential storage structure. To discuss a two-dimensional array element in memory below that standard mapping between array and memory addresses.

Disposed two-dimensional array of data elements occupied by each L units, m, n is the number of rows and columns of the array, Loc (a11) represented by the element address is stored in row-major - basic idea is stored by row, i.e., a deposit then i-th row is then stored first line 1 + i, as follows.
Here Insert Picture Description
Row major order address calculation priority storage
There are ((i-1) * n + j-1) prior to aij element
storage address aij of = Loc (aij) = Loc (A11) + ((I-. 1) * n-+ J -1) * L
Here Insert Picture Description
Row major order address calculation priority storage
aij before Total ((j-1) * m + i-1) th element
storage address of aij = Loc (aij) = Loc ( a11) + ((j-1) * m + i -1) * L

Above array stored sequentially in two ways, as long as the nodes know the start address for storing points (base address), the dimensions and the dimensions, the lower bound, and the number per unit occupied by each array element, it can be an array of elements store the address represents a linear function of its target under. Sequential storage array is a random access structure.

Array elements addressed is
provided an array a [1 ... 60, 1 ... 70] of the base address 2048, accounting for each element of the storage unit 2, if the main column sequence order stored sequentially seeking elements a [32,58] of memory address.
Note: a [1 ... 60, 1 ... 70] is an array representation, showing a two-dimensional array in row-major, the row index in the range of 1 to 60, the column index in the range of 1-70.

解:
数组行数m=60-1+1=60; 列数n=70-1+1=70;
行下标i=32; 列下标j=58; 元素长度L=2;
列优先元素求址公式Loc(aij)=Loc(a11)+[(j-1)*m+(i-1)]*L
得:LOC(a32,58)=2048+[(58-1)*60+(32-1)]*2=8950

若数组是a[0…59, 0…69],结果是否仍为8950?

i、j均从0开始,求址公式为Loc(aij)=Loc(a00)+[j*m+i)]L
LOC(a32,58)=2048+[58
60+32]*2=9072
Here Insert Picture Description

2.矩阵的压缩存储

把相同的数据只存储一次,不但可以节省存储空间,还可以节约传输时间。

特殊矩阵:
值相同元素或者零元素分布有一定规律的矩阵
Here Insert Picture Description
对称矩阵:上三角区域和下三角区域的值相同;
上三角矩阵:下三角部分的值都相同(图中的 c 表示同一个值,^符号表示省略的元素)。

矩阵压缩存储时会有什么问题?可以采用的方法是什么?

一是应该存什么样的数据及如何存储,二是保存的数据要能恢复原样。

问题一的分析与处理:
(1)相同的数据只存储一次;(2)零元素不占存储空间。
可以把要存储的数据放到一维数组中。

问题二的分析与处理:
找到同一元素在一维数组与矩阵中的对应关系即可恢复原有的矩阵形式。

矩阵压缩存储方法:
步骤1
确定存放压缩后数据的向量的空间大小;
步骤2
确定二维数组下标 i, j 与向量下标 k 的关系。

对称矩阵的压缩存储:

对称矩阵压缩存储方法:
用向量存储,对称矩阵元素可以只存储下或上三角部分

按“行优先顺序”存储主对角线(包括对角线)以下的元素,以一维数组 sa[M] 作为 n 阶对称矩阵 A 的存储结构,要找到 A 中任意一元素 aij 与它的存储位置 sa[k] 之间存在的对应关系。
Here Insert Picture Description
共需要存储的元素数目M为:n(1+n)/2
Symmetric matrix storage mapping relationship
k等于aij前的元素个数(此处k的值从0开始):
k =1+2+3+…+i+j = i(1+i)/2+j (i≥j)

三角矩阵的压缩存储

三角矩阵压缩存储方法:
除了存储主对角线及上(下)三角中的元素外,再加一个存储常数 c 的空间。
三角矩阵中的重复元素 c 可只用一个存储单元,其余的元素有 n(n+1)/2 个,因此,三角矩阵可压缩存储到长度为 n(n+1)/2 + 1 的向量中,其中 c存放在向量的最后位置,矩阵下标与向量下标间的关系如图:
Here Insert Picture Description
对角矩阵的压缩存储

对角矩阵 :

所有非零元素都集中在以主对角线为中心的带状区域中的方阵,也称为带型矩阵。

对角矩阵压缩存储方法:

存储所有非零元素
Here Insert Picture Description
三对角矩阵压缩方法一:
设有n阶三对角矩阵A[n][n],将三条对角线上的元素逐行存放于数组B[M]中,使得B[k]=A[i][j], 给出i、j与k的对应关系。

除第 1 行和第 n 行是 2 个元素外,每行的非零元素都是 3 个,因此,需存储的元素个数 M = 3n-2。

k=ai+bj+c (a、b、c分别为整数)
Matrix subscripts i, j and k value of the subscript vector table
将i、j、k的取值带入方程,可求得a、b、c的值。
解得:a=2; b=1; c=0;
所以:k=2i+j —— (0<=i<n; 0<=j<n)
Tridiagonal matrices compressed relationship i, j and k,
i=(k+1)/ 3 ——(0<=k<3n-2)
j=(k+1)/ 3+(k+1)% 3 -1
或者 j=k-2i=k-[(k+1)/3]*2 (注意除法是整除不可化简)

三对角矩阵压缩方法二 :

只存储带内的元素。
Diagonal matrix like bandwidth storage
4.稀疏矩阵的压缩存储

稀疏矩阵:
设矩阵Amn中有s个非零元素,若s远远小于矩阵元素的总数(即s<<m×n),则称A为稀疏矩阵(sparse matrix)。

矩阵的稀疏因子:
令 e=s/(m*n),称e为矩阵的稀疏因子。
通常认为e≦0.05时称之为稀疏矩阵。

稀疏矩阵压缩存储原则:
存储所有非零元素 。

稀疏矩阵常用压缩存储形式:
1)三元组表
存储原则:三元组表存稀疏矩阵中各非零元的值、行列位置和矩阵的行列数。

【例】用三元组表的形式存储稀疏矩阵。
解:设稀疏矩阵为M[6,7],为方便管理,可以把矩阵的行、列、非零元素个数信息,专门用三元组的第0行来记录。
Here Insert Picture Description
三元组的数据结构类型描述:
Here Insert Picture Description
结点结构设计:

struct node
{ int  row,col;         // 非零元素的行下标和列下标
  int value;             // 非零元素值
}; 
typedef struct node NODE;
NODE matrix[MAX]

2)稀疏矩阵的链表结构

原因:在运算中若非0元素的位置发生变化,会引起数组元素的频繁移动,三元组表就不适合做稀疏矩阵的存储结构。

链式存储根据结点间链接方式的不同,可以有多种形式:
(1)带行指针向量的单链表表示法
Here Insert Picture Description
每一个非零元素用一个结点表示,矩阵的每一行的非零元素用一个单链表存储。结点 struct node 成员 col 记录元素所在的列信息, value 记录元素值;所有链表的头地址信息汇集在数组 TAB[ROW] 中。

结点结构设计:

typedef  struct  node
{  int  col , value ;
   struct  node  *next;
}   linklist;
linklist *TAB[ROW];
#define ROW 4

(2)十字链表的存储结构

在十字链表中,表示非0元素的结点除了三元组,还有两个指针域:向下域(down)链接同一列下一个非0元素,链接成一个带头结点的列循环链表;向右域(right)链接同一行下一个非0元素,链接成一个带头结点的行循环链表。
Here Insert Picture Description
Here Insert Picture Description
3.字符串

1)字符串的定义:

串是一种特殊的线性表,它是由n (≥ 0)个字符组成的有限序列

记作 s = “a1,a2, a3, … an”
s----串名, a1,a2, a3, … an----串值
ai是串中字符,n是串的长度

串的例子:
Here Insert Picture Description
串的有关术语:
Here Insert Picture Description
串的基本操作:
(1)字符串的长度计算
(2)字符串的复制
(3)字符串的连接
(4)字符串的替换
(5)字符串的插入
(6)字符串的删除
(7)字符串的比较
(8)抽取字符串
(9)字符串的分割
(10)字符串的查找

由于在许多高级语言中都提供相应的串操作处理功能,故对串的操作不再赘述。

2)字符串的存储结构

(1)串的顺序存储

串的顺序存储——方案1:用一个指针来指向最后一个字符。

Here Insert Picture Description
数据结构设计:

typedef struct
{    char ch[MAXSIZE];
      int curlen;
}    SeqString;

串的顺序存储——方案2:直接记录串长
Here Insert Picture Description
结构描述:char s[MAXSIZE+1];

串的顺序存储——方案3:在串尾存储一个不会在串中出现的特殊字符作为串的终结符
Here Insert Picture Description
结构描述:char s[MAXSIZE+1];

串的顺序存储特点:
(1)预定义串的最大长度;
使得串的某些操作受限(截尾),如串的连接、插入、置换等运算;

(2)插入和删除操作不方便;
需要移动大量的字符

2)串的块链存储结构
Here Insert Picture Description
单字符链串存储结构设计:

typedef struct linknode
{ char data;
  struct linknode *next;
} linkstring;

每个链结点中只放一个字符,插入、删除、求长度等运算非常方便,但存储效率低。

改进的方法,可以在一个链结点中存储多个字符,这样就可以改善存储效率,在处理不定长的大字符串时很有效,这是顺序串和链串的综合折中,称为块链结构。
Here Insert Picture Description
串的块链存储结构:

typedef struct linknode
{ char data[4];
  struct linknode *next;
} linkstring;

3)串的索引存储方式

用串变量的名字作为关键字组织起来的索引表,表中串名与串值之间一一对应。

(1)带长度的串索引表

设:S1=“please”,S2= “seek”.

把每个串的首地址和长度记录下来,放在索引表——数据结构中,便于管理。
Here Insert Picture Description
结构描述:

typedef struct 
{ 
    char name[maxsize];//串名
    int length;                 //串长度
    char *stadr;              //串起始地址
} lnode

(2)带头尾指针的串索引表
设:S1=“abcdefg”, S2= “bcd”
一个串设置两个指针,一个指向串开头,一个指向串末尾

Here Insert Picture Description
结构描述:

typedef struct
{ 
    char name[maxsize];//串名
    char *stadr, //串头地址
    char *enadr; //串尾地址
} enode;

(3)带特征位的串索引表

设:S1=“abcdefg” S2= “bcd”
在索引表中设置一标志量tag来标示存储的是串地址还是串内容,这样设计可以让较短的串存取便捷一些。
Here Insert Picture Description
结构描述:

typedef struct 
{ 
       char  name[maxsize];
       int  tag;                 // 特征位
       union //共用体变量
      {   
             char *stadr;      // 特征位为0,放串首地址
             char value[4];  //特征位为1,放串值
      } uval;
} tagnode

(4) chain string index table
is provided: s1 = "GOOD" s2 = "DAY"
Here Insert Picture Description

 //串链结点定义
typedef struct linknode
{ char data;
  struct linknode *next;
} linkstring;

// 串链数组定义
typedef struct        
{    char name[maxsize]; //串名
     linkstring *link;
}  linkstru;
linkstru aStr [N];

Here Insert Picture Description

Published 26 original articles · won praise 3 · Views 1474

Guess you like

Origin blog.csdn.net/herui7322/article/details/104105786