线性表
线性表是n个具有相同特性的数据元素的有限序列
- 常见顺序表:顺序表、链表、栈、队列、字符串
- 线性表在逻辑上是线性结构,也就说是连续的一条直线
- 线性表在物理结构上并不一定是连续的
- 线性表在物理上存储时,通常以数组和链式结构的形式存储。
- 线性表:数据在逻辑上有前后关系
顺序表
顺序表:用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般采用数组存储
- 顺序表的存储位置和逻辑关系是吻合的
class ArrayList;
class MyArrayList{
int[] array; //实际空间
int size; //实际数据
}
/**
*头插
* 1.需要移动size个元素
* 2.为避免数据被覆盖,从最后一个元素开始移动
* 3.循环范围:空间下标为基准 [size,1] 数据下标为基准 [size-1,0]
* 4.array[空间] = array [数据]
* 5.空间 = 数据+1
*/
public void pushFront(int item) {
for(int i = this.size; i >= 1; i--){
this.array[i] = this.array[i - 1]; //从最后一个元素开始移动
}
this.array[0] = item;
this.size++;
}
/**
* 尾插O(1)
*/
public void pushBack(int item) {
this.array[this.size] = item; //新元素直接放最后
this.size++;
}
/**
* 按下标位置插O(n)
* 空间的下标 [this.size,inidex+1] [size,index)
* 数据的下标 [size -1,index]
* array[空间] = array[数据]
*/
public void add(int item, int index) {
if(index < 0 || index > this.size){
throw new Error(); //越界
}
//index=0--头插 index=this.size--尾插
for(int i = this.size - 1; i >= index; i--){
this.array[i + 1] = this.array[i]; //从后往前遍历,数组元素后移i-->i+1
}
this.array[index] = item;
this.size++;
}
/**
* 头删O(n)
* 1.一共要移size-1
* 2.从前往后
* 3.数据[1,size-1] **空间[0,size-2]**
*/
public void popFront() {
if(this.size == 0){
throw new Error();
}
for(int i = 0; i < this.size - 1; i++){
this.array[i] = this.array[i + 1];
}
this.array[--this.size] = 0; //this.size--;
}
/**
* 尾删O(1)
*/
public void popBack() {
if(this.size == 0){
throw new Error();
}
this.array[--this.size] = 0;
}
/**
* 根据下标位置删O(n)
*/
public void remove(int index) {
if(index < 0 || index >= this.size){
throw new Error();
}
if(this.size == 0){
throw new Error();
}
//从下标位置起始
for(int i = index ; i > this.size - 2 ; i--){ //注意:this.size-2
this.array[i] = this.array[i+1];
}
this.array[--this.size] = 0;
}
/*
*解释this.size-2:
*1.后面的元素往前移一位-->this.size-1
*2.除去要删除的index所指的元素-->this.size-2
*/
/**
* 数组扩容O(n)
* 扩容:保证数组容量够用
* 基本思路:
* 先判断数组容量是否够用:
* 1.不够用将容量扩至原来的两倍
* 2.将元素转移到扩容后的新数组中
* 3.用新数组替代原数组
*/
private void ensureCapacity() {
if(this.size < this.array.length){
return; //够用无需扩容
}
//不够用则扩容至原来两倍
int capacity = this.array.length*2;
int [] newArray = new int[capacity];
//转移元素
for(int i = 0; i < this.size; i++){
newArray[i] = this.array[i];
}
//替代原数组
this.array = newArray;
}
/*这一步骤应该在每次对顺序表进行更改时都调用以保证数组容量够用*/
注意:
/*
简单来讲,扩容就好比找房子
this.array-->住的老房子
this.size-->房子里住的人
搬家条件:this.size == this.array.length (不用考虑大于的情况)
1.先找新房子,找原来的两倍大
2.搬家
3.this.array = 新房子的地址
4.退掉老房子 (JVM自行处理)
*/
链表
链表:链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的引用链 接次序实现的 。
- 链表逻辑上有线性关系,物理存储不保证连续
常用的三种链表结构:
- 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结
构,如哈希桶、图的邻接表等等。 - 带头循环单链表:结构较无头单向非循环链表简单。实际操作当中使用较少。
- 不带头双向循环链表:在Java的集合框架库中LinkedList底层实现就是不带头双向循环链表。
/**
* 链表的基本结构
* 结点{ 值,下一个结点的引用 }
*/
Node{ value , next }
class Node{
public int value; //保存的是有效数据
public Node next; //下一个结点的线索(引用)
}
/**
*头插O(1)
* 1.新结点的next指向head
* 2.将新结点赋给head
*/
void pushFront(int item){
node.next = this.head;
this.head = node;
}
/**
* 尾插
* 判断链表是否为空
* 1.为空,直接插入元素
* 2.不为空,先找到最后一个结点,再将它的next指向新结点
*/
//获取最后一个结点O(n)
private Node getLast(){
Node cur = this.head; //定义结点变量,初始值为head
while(cur.next != null){
cur = cur.next;
}
return cur;
}
//尾插O(n)
public void pushBack(int item) {
Node node = new Node(item);
if(this.head == null){
this.head = node;
}
Node last = getLast();
last.next = node;
}
/**
*头删O(1)
*/
public void popFront() {
if(this.head == null){
throw new Error();
}
this.head = this.head.next;
}
/**
* 尾删
* 1.先判断链表长度:为空则异常,只有一个元素则直接删,元素个数>=2如下:
* 2.链表中元素个数 >= 2时,将倒数第二个元素的next置为null
*/
//获取倒数第二个元素O(n)
private Node getLastSecond() {
Node cur = this.head;
while(cur.next.next != null){
cur = cur.next;
}
return cur;
}
//尾删O(n)
public void popBack() {
if(this.head == null){
throw new Error();
}
//链表中只有一个元素
if(this.head.next == null){
this.head = null;
}else{
Node lastSecond = getLastSecond();
lastSecond.next = null;
}
}
- 顺序表和链表的区别
顺序表
- 优点:空间连续,支持随机访问
- 缺点:中间或前面部分的插入删除时间复杂度为O(n) ,而且增容的代价比较大。
链表
- 优点:任意位置插入删除时间复杂度为O(1),而且没有增容问题,插入一个元素则开辟一个空间。
- 缺点:以节点为单位存储,不支持随机访问
顺序表完整代码移步:https://github.com/Loinbo/Data-Structure/tree/master/Search
链表完整代码移步:https://github.com/Loinbo/Data-Structure/tree/master/MyLinkedList1