|
1 | | -{\rtf1} |
| 1 | +# 线性表 |
| 2 | + |
| 3 | +线性表是具有**相同特性**数据元素的一个**有限**序列。该序列中所含元素的个数叫做线性表的长度,用 n(n>=0)表示。其中,n 可以等于零,表示线性表是一个空表,空表也可以是一个线性表。 |
| 4 | + |
| 5 | +注意:线性表是**逻辑概念**,表示元素之间一对一的相邻关系。 |
| 6 | + |
| 7 | +## 线性表的顺序表示 |
| 8 | + |
| 9 | +线性表的顺序存储称为**顺序表**。 |
| 10 | + |
| 11 | +用一组地址连续的存储单元,依次存储线性表中数据元素,从使得逻辑上相邻的两个元素在物理位置上也相邻。 |
| 12 | + |
| 13 | +注意:线性表中元素的位序是从 1 开始的,而数组中元素的下标是从 0 开始的。 |
| 14 | + |
| 15 | +### 顺序表的结构定义 |
| 16 | + |
| 17 | +```java |
| 18 | +public class SeqList { |
| 19 | + |
| 20 | + private int[] data; |
| 21 | + private int size; |
| 22 | + |
| 23 | + public SeqList(int capacity){ |
| 24 | + data=new int[capacity+1]; |
| 25 | + //注意:线性表中元素的位序是从 1 开始的,而数组中元素的下标是从 0 开始的。 |
| 26 | + size=0; |
| 27 | + } |
| 28 | + |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +### 顺序表的算法操作 |
| 35 | + |
| 36 | +#### 1. 插入操作 |
| 37 | + |
| 38 | +- 代码 |
| 39 | + |
| 40 | + ```java |
| 41 | + //在 index 位置插入元素 |
| 42 | + public void add(int index,int e){ |
| 43 | + if(size==data.length-1){ //注意:数组 0 位置不放元素 |
| 44 | + throw new IllegalArgumentException("Add failed,array is full"); |
| 45 | + } |
| 46 | + if(index<1 || index>size+1){ |
| 47 | + throw new IllegalArgumentException("Add Failed,1<=index<=size is required"); |
| 48 | + } |
| 49 | + //index位置后的元素向右移动 |
| 50 | + for(int i=size;i>index;i--){ |
| 51 | + data[i]=data[i-1]; //元素右移语句 |
| 52 | + } |
| 53 | + data[index]=e; |
| 54 | + size++; |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | +- 复杂度分析 |
| 59 | + |
| 60 | + - 最好情况:在表尾插入元素,元素右移语句将不执行,时间复杂度 O(1) |
| 61 | + |
| 62 | + - 最坏情况:在表头插入元素,元素右移语句执行 n 次,时间复杂度 O(n) |
| 63 | + |
| 64 | + - 平均情况:在 i 位置插入一个节点的概率为 1/(n+1)(因为有 n+1 个位置可以随机插入),则在长度为 n 的线性表中插入一个节点时所需移动节点的平均次数为 |
| 65 | + $$ |
| 66 | + \sum_{i=1}^{n+1}\frac{1}{n+1}(n-i+1)=\frac{1}{n+1}\sum_{i=1}^{n+1}(n-i+1)=\frac{n}{2} |
| 67 | + $$ |
| 68 | + 所以,时间复杂度是 O(n)。 |
| 69 | + |
| 70 | + |
| 71 | +#### 2. 删除操作 |
| 72 | + |
| 73 | +- 代码 |
| 74 | + |
| 75 | + ```java |
| 76 | + //删除指定位置元素 |
| 77 | + public int remove(int index){ |
| 78 | + if(size==0){ |
| 79 | + throw new IllegalArgumentException("Remove failed,array is empty"); |
| 80 | + } |
| 81 | + if(index<1 || index>size){ |
| 82 | + throw new IllegalArgumentException("Remove failed,index is illegal"); |
| 83 | + } |
| 84 | + int ret=data[index]; |
| 85 | + for(int i=index+1;i<size;i++){ |
| 86 | + data[i-1]=data[i]; |
| 87 | + } |
| 88 | + size--; |
| 89 | + return ret; |
| 90 | + } |
| 91 | + ``` |
| 92 | + |
| 93 | +- 复杂度分析 |
| 94 | + |
| 95 | + - 最好情况:删除表尾元素,无需移动元素,时间复杂度为 O(1) |
| 96 | + |
| 97 | + - 最坏情况:删除表头元素,需要移动一个元素外的所有元素,时间复杂度为 O(n) |
| 98 | + |
| 99 | + - 平均情况:在 i 位置删除一个节点的概率为 1/n,则在长度为 n 的线性表中删除一个节点时所需移动节点的平均次数为 |
| 100 | + $$ |
| 101 | + \sum_{i=1}^{n}\frac{1}{n}(n-i)=\frac{1}{n}\sum_{i=1}^{n}(n-i)=\frac{n-1}{2} |
| 102 | + $$ |
| 103 | + 所以,时间复杂度是 O(n)。 |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +#### 3. 按值查找 |
| 108 | + |
| 109 | +- 代码 |
| 110 | + |
| 111 | +```java |
| 112 | +//查找数组中元素e所在位置 |
| 113 | +public int find(int e){ |
| 114 | + for(int i=0;i<size;i++){ |
| 115 | + if(data[i]==e){ |
| 116 | + return i; |
| 117 | + } |
| 118 | + } |
| 119 | + return -1; |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +- 时间复杂度 |
| 124 | + |
| 125 | + - 最好情况:查找的元素就在表头,仅需比较一次,时间复杂度为 O(1)。 |
| 126 | + |
| 127 | + - 最坏情况:查找的元素在表尾(或者元素不存在),需要比较 n 次数,时间复杂度为 O(n)。 |
| 128 | + |
| 129 | + - 平均情况:查询元素在 i 位置上的概率为 1/n,则在长度为 n 的线性表中查找值为 e 的元素所需比较的平均次数为 |
| 130 | + $$ |
| 131 | + \sum_{i=1}^{n}\frac{1}{n}i=\frac{1}{n}\sum_{i=1}^{n}i=\frac{n+1}{2} |
| 132 | + $$ |
| 133 | + 所以,时间复杂度为 O(n)。 |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +## 线性表的链式表示 |
| 138 | + |
| 139 | +线性表的链式存储称为**单链表**。 |
| 140 | + |
| 141 | +单链表是指通过一组任意的存储单元来存储线性表中的数据,为了建立数据元素之间的线性关系,对每个链表节点,除了存放元素的自身信息外,还需要存放一个指向其后继的指针。 |
| 142 | + |
| 143 | +### 单链表的结构定义 |
| 144 | + |
| 145 | +```java |
| 146 | +class Node{ |
| 147 | + int e; |
| 148 | + Node next; |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +### 单链表的算法操作 |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +<div align="center"><img src="https://gitee.com/duhouan/ImagePro/raw/master/java-notes/dataStructure/ds_2.png" width="500px"/></div> |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +<div align="center"><img src="https://gitee.com/duhouan/ImagePro/raw/master/java-notes/dataStructure/ds_3.png" width="500px"/></div> |
0 commit comments