基本概念
线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。
线性表的基本特征:
- 第一个数据元素没有前驱元素;
- 最后一个数据元素没有后继元素。
- 其余每个数据元素只有一个前驱元素和一个后继元素;
抽象数据类型:
线性表一般包括插入、删除、查找等基本操作。
代码如下:
public interface List<Item> {
//线性表的大小
public int size();
//判断线性表是否为空
public boolean isEmpty();
//添加新元素
public void add(Item item);
//读取指定位置的元素
public Item get(int index) throws Exception;
//插入元素
public void insert(int index,Item item) throws Exception;
//删除第index个元素
public Item delete(int index) throws Exception;
}
线性表的分类:
线性表按物理存储结构的不同可分为顺序表(顺序存储)和链表(链式存储)。
- 顺序表(存储结构连续,数组实现)
- 链表(存储结构上不连续,逻辑上连续)
顺序表
顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。
其插入删除操作如图所示:
代码如下:
/**
* 顺序表
* @author Gain
*
*/
public class SequentialList<Item> implements List<Item>{
private int N;//记录元素个数
private Item[] data;
//指定顺序表的最大长度
SequentialList(int num) {
data = (Item[])new Object[num];
}
public int size() {return N;}
public boolean isEmpty() {return N==0;}
public void add(Item item) {
if(N >= data.length-1)
return;
data[N] = item;
N++;
}
public Item get(int index) throws Exception {
//判断下标是否越界
if(index<1||index>N)
throw new Exception("参数越界");
return data[index-1];
}
//插入元素
public void insert(int index,Item item) throws Exception{
if(index<0||index>N)
throw new Exception("参数越界");
//将元素后移
for(int i=0;i<N-index;i++) {
data[N-i] = data[N-1-i];
}
data[index] = item;
N++;
}
//删除第index个元素
public Item delete(int index) throws Exception{
if(index<1||index>N)
throw new Exception("参数越界");
Item item = data[index-1];
//将元素前移
for(int i=0;i<N-index;i++) {
data[index-1+i] = data[index+i];
}
N--;
return item;
}
}
注意:
- 插入操作:移动元素时,要从后往前操作,不能从前往后操作,不然元素会被覆盖的。
- 删除元素:移动元素时,要从前往后操作。
测试代码如下:
public class Test {
public static void main(String[] args) throws Exception{
SequentialList<Integer> sl = new SequentialList<Integer>(20);
for(int i=0;i<10;i++) {
sl.add(i);
}
System.out.println("删除1位置元素:"+sl.delete(1));
sl.insert(0,15);
for(int i=1;i<=sl.size();i++) {
System.out.print(sl.get(i)+" ");
}
}
}
运行结果:
分析:
顺序表效率分析:
- 顺序表插入和删除一个元素的时间复杂度为O(n)。
- 顺序表支持随机访问,读取一个元素的时间复杂度为O(1)。因为我们是可以通过下标直接访问的,所以时间复杂度是固定的,和问题规模无关。
链表
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
链表有带头结点结构和不带头结点结构两种,其结构如下图
头指针(head引用)所指的不存放数据元素的第一个结点称作头结点(头结点指向首元结点)。头结点的数据域一般不放数据(也可存放链表的长度、用做监视哨等),此结点不能计入链表长度值。存放第一个数据元素的结点称作首元结点。
带头结点的链表具有两个优点:
- 在链表的第一个位置上的操作(在第一个元素结点前插入结点和删除第一个结点)和表的其它位置上操作一致,无须进行特殊处理;
- 无论链表是否为空,head一定不为空,因此空表和非空表的处理也就统一了。
带头结点的链表插入与删除示意图:
代码如下:
/**
* 带头结点的链表
* @author Gain
*/
public class LinkedList<Item> implements List<Item>{
private Node head;
private int N;
private class Node {
Item data;
Node next;
}
LinkedList() {
head = new Node();
}
public int size() {return N;}
public boolean isEmpty() {return N==0;}
public void add(Item item) {
Node newNode = new Node();
newNode.data = item;
Node current = head;
while(current.next != null) {
current = current.next;
}
current.next = newNode;
N++;
}
//找到第index个结点
public Node location(int index) throws Exception{
if(index<0||index>N)
throw new Exception("参数越界");
Node current = head;
for(int i=0;i<index;i++) {
current = current.next;
}
return current;
}
public Item get(int index) throws Exception {
return location(index).data;
}
//在index之后插入元素
public void insert(int index, Item data) throws Exception {
Node newNode = new Node();
newNode.data = data;
Node current = location(index);
Node node = current.next;
current.next = newNode;
newNode.next = node;
N++;
}
//删除第index个元素
public Item delete(int index) throws Exception {
Node node = location(index-1);
Item data = node.next.data;
node.next = node.next.next;
N--;
return data;
}
}
测试代码如下:
public class Test {
public static void main(String[] args) throws Exception{
LinkedList<Integer> ll = new LinkedList<Integer>();
for(int i=0;i<10;i++) {
ll.add(i);
}
System.out.println("删除1位置元素:"+ll.delete(1));
ll.insert(0,15);
for(int i=1;i<=ll.size();i++) {
System.out.print(ll.get(i)+" ");
}
}
}
运行结果:
分析:
链表插入和删除操作的时间复杂度均为O(n)。另外,链表读取数据元素操作的时间复杂度也为O(n)。
比较
顺序表:
- 优点:主要优点是支持随机读取,取数据操作的时间复杂度为O(1)。内存空间利用效率高;
- 缺点:主要缺点是需要预先给出数组的最大数据元素个数,而这通常很难准确作到。当实际的数据元素个数超过了预先给出的个数,会发生异常。另外,顺序表插入和删除操作时需要移动较多的数据元素。
链表:
- 优点:不需要预先给出数据元素的最大个数,单链表插入和删除操作时不需要移动数据元素;
- 缺点:主要缺点是每个结点中要有一个指针,因此单链表的空间利用率略低于顺序表的。另外,单链表不支持随机读取,单链表取数据元素操作的时间复杂度为O(n)。