链表
虽然顺序表的查询很快,时间复杂度为O(1),但是增删的效率是比较低的,因为每一次增删操作都伴随着大量的数据元素移动。这个问题有没有解决方案呢?有,我们可以使用另外一种存储结构实现线性表,链式存储结构。
链表是一种物理存储单元上非连续、非顺序的存储结构,其物理结构不能只管的表示数据元素的逻辑顺序,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列的结点(链表中的每一个元素称为结点)组成,结点可以在运行时动态生成。
按照面向对象的思想,我们可以设计一个类,来描述结点这个事物,用一个属性描述这个结点存储的元素,用来另外一个属性描述这个结点的下一个结点。
类名 | Node |
---|---|
构造方法 | Node(T t,Node next):创建Node对象 |
成员变量 | T item:存储数据 Node next:指向下一个结点 |
结点类实现:
public class Node<T> {
//存储元素
public T item;
//下一个元素的地址
public Node next;
public Node (T item,Node next){
this.item = item;
this.next = next;
}
}
生成链表:
public static void main(String[] args) throws Exception{
//构建结点
Node<Integer> first = new Node<>(11,null);
Node<Integer> second = new Node<>(13,null);
Node<Integer> third = new Node<>(12,null);
Node<Integer> forth = new Node<>(7,null);
Node<Integer> fifth = new Node<>(8,null);
//生成链表
first.next = second;
second.next = third;
third.next = forth;
forth.next = fifth;
}
单向链表
单向链表是链表的一种,它由多个结点组成,每个结点都由一个数据域和一个指针域组成,数据域用来存储数据,指针域用来指向其后继结点。链表的头结点的数据域不存储数据,指针域指向第一个真正存储数据的结点。
单向链表API设计
类名 | LinkList |
---|---|
构造方法 | LinkList():创建LinkList对象 |
成员方法 | 1.public void clear():空置线性表 2.publicboolean isEmpty():判断线性表是否为空,是返回true,否返回false 3.public int length():获取线性表中元素的个数 4.public T get(int i):读取并返回线性表中的第i个元素的值 5.public void insert(T t):往线性表中添加一个元素; 6.public void insert(int i,T t):在线性表的第i个元素之前插入一个值为t的数据元素。 7.public T remove(int i):删除并返回线性表中第i个数据元素。 8.public int indexOf(T t):返回线性表中首次出现的指定的数据元素的位序号,若不存在,则返回-1。 |
成员内部类 | private class Node:结点类 |
成员变量 | 1.private Node head:记录首结点 2.private int N:记录链表的长度 |
单向链表代码实现
单向链表类:
import java.util.Iterator;
public class LinkList<T> implements Iterable<T> {
//头节点
private Node head;
//链表长度
private int N;
public LinkList(){
//初始化
head = new Node(null,null);
N = 0;
}
//清空链表
public void clear(){
head.next = null;
head.item = null;
N = 0;
}
//判断是否为空
public boolean isEmpty(){
return N == 0;
}
//获取元素个数
public int length(){
return N;
}
//获取第i个元素
public T get(int i){
if (i < 0 || i >= N){
throw new RuntimeException("位置不合法");
}
Node n = head.next;
for (int index = 0;index < i;index++){
n = n.next;
}
return n.item;
}
//添加一个元素
public void insert(T t){
//找到最后一个节点
Node n = head;
while (n.next != null){
n = n.next;
}
Node newOne = new Node(t,null);
n.next = newOne;
//链表长度++
N++;
}
//在i位置添加一个元素
public void insert(int i,T t){
if (i<0||i>=N){
throw new RuntimeException("位置不合法!");
}
//寻找i位置之前的节点
Node pre = head;
for (int j = 0; j <= i-1; j++) {
pre = pre.next;
}
//找到位置i的节点
Node curr = pre.next;
//构建新节点,让新节点指向i节点
Node newNode = new Node(t,curr);
//让i位置之前的节点指向这个节点
pre.next = newNode;
//链表长度++
N++;
}
//删除并返回第i个位置的元素
public T delete(int i){
if (i<0||i>=N){
throw new RuntimeException("位置不合法!");
}
//寻找i之前的元素
Node pre = head;
for (int index = 0; index <=i-1; index++) {
pre = pre.next;
}
//当前i位置的结点
Node curr = pre.next;
//前一个结点指向下一个结点,删除当前结点
pre.next = curr.next;
//长度-1
N--;
return curr.item;
}
//返回链表中首次出现指定元素的序号,弱不存在返回-1
public int indexOf(T t){
Node n = head;
for (int i = 0;n.next!=null;i++){
n = n.next;
if (n.item.equals(t)){
return i;
}
}
return -1; }
//节点类
private class Node{
//存储数据
T item;
//下个结点
Node next;
public Node(T item,Node next){
this.item = item;
this.next = next;
}
}
@Override
public Iterator iterator() {
return new LIterator();
}
private class LIterator implements Iterator<T>{
private Node n;
public LIterator(){
this.n = head;
}
@Override
public boolean hasNext() {
return n.next != null;
}
@Override
public T next() {
n = n.next;
return n.item;
}
}
}
测试类:
public class LinkListTest {
public static void main(String[] args) throws Exception{
LinkList<Integer> list = new LinkList<>();
System.out.println(list.length());
list.insert(0,1);
list.insert(1,2);
list.insert(2,3);
list.insert(3,4);
list.insert(4,5);
//测试length方法
for (int i : list) {
System.out.println(i);
}
System.out.println(list.length());
System.out.println("-------------------");
//测试get方法
System.out.println(list.get(2));
System.out.println("------------------------");
//测试remove方法
int remove = list.remove(1);
System.out.println(remove);
System.out.println(list.length());
System.out.println("----------------");
for (int i : list) { System.out.println(i); }
}
}
0
1
2
3
4
5
5
---------------
3
---------------
2
4
---------------
1
3
4
5
双向链表
双向链表也叫双向表,是链表的一种,它由多个结点组成,每个结点都由一个数据域和两个指针域组成,数据域用来存储数据,其中一个指针域用来指向其后继结点,另一个指针域用来指向前驱结点。链表的头结点的数据域不存储数据,指向前驱结点的指针域值为null,指向后继结点的指针域指向第一个真正存储数据的结点。
结点API设计
类名 | Node |
---|---|
构造方法 | Node(T t,Node pre,Node next):创建Node对象 |
成员变量 | T item:存储数据 Node next:指向下一个结点 Node pre:指向上一个结点 |
双向链表API设计
类名 | TowWayLinkList |
---|---|
构造方法 | TowWayLinkList():创建TowWayLinkList对象 |
成员方法 | 1.public void clear():空置线性表 2.publicboolean isEmpty():判断线性表是否为空,是返回true,否返回false 3.public int length():获取线性表中元素的个数 4.public T get(int i):读取并返回线性表中的第i个元素的值 5.public void insert(T t):往线性表中添加一个元素; 6.public void insert(int i,T t):在线性表的第i个元素之前插入一个值为t的数据元素。 7.public T remove(int i):删除并返回线性表中第i个数据元素。 8.public int indexOf(T t):返回线性表中首次出现的指定的数据元素的位序号,若不存在,则返回-1。 9.public T getFirst():获取第一个元素 10.public T getLast():获取最后一个元素 |
成员内部类 | private class Node:结点类 |
成员变量 | 1.private Node first:记录首结点 2.private Node last:记录尾结点 2.private int N:记录链表的长度 |
双向链表代码实现
双向链表类:
import java.util.Iterator;
public class TwoWayLinkList<T> implements Iterable<T> {
//结点类
private class Node{
public Node(T item, Node pre, Node next){
this.item = item;
this.pre = pre;
this.next = next;
}
//存储数据
private T item;
//存储上一个结点
private Node pre;
//存储下一个节点
private Node next;
}
//首结点
private Node head;
//尾结点
private Node last;
//链表长度
private int N;
//构造方法
public TwoWayLinkList(){
last = null;
head = new Node(null,null,null);
N = 0;
}
//清空链表
public void clear(){
N = 0;
last = null;
head.next = last;
head.pre = null;
head.item = null;
}
//判断是否为空
public boolean isEmpty(){
return N==0;
}
//获取元素个数
public int length(){
return N;
}
//获取位置i的元素值
public T get(int i){
if (i<0 || i>= N){
throw new RuntimeException("位置不合法");
}
Node curr = head.next;
for (int j = 0; j < i; j++) {
curr = curr.next;
}
return curr.item;
}
//添加一个元素在末尾
public void insert(T t){
if(last == null){
last = new Node(t,head,null);
head.next = last;
}else{
Node oldlast = last;
Node node = new Node(t,oldlast,null);
oldlast.next = node;
last = node;
}
N++;
}
//在i位置添加一个元素
public void insert(int i, T t){
if (i<0 || i>=N){
throw new RuntimeException("位置不合法");
}
//找到i位置的前一个结点
Node pre = head;
for (int index=0; index < i;index++){
pre = pre.next;
}
//设置当前结点
Node curr = pre.next;
//构建这个结点
Node newNode = new Node(t,pre,curr);
curr.pre = newNode;
pre.next = newNode;
//长度++
N++;
}
//删除并返回第i个元素
public T remove(int i){
if (i<0 || i>=N){
throw new RuntimeException("位置不合法");
}
//找到前一个结点
Node pre = head;
for (int index=0; index < i;index++){
pre = pre.next;
}
//设置当前结点
Node curr = pre.next;
//找到后一个结点
Node curr_next = curr.next;
pre.next = curr_next;
curr_next.pre = pre;
N--;
return curr.item;
}
//返回表中某元素首次出现的index,如果不存在就返回-1
public int indexOf(T t){
Node n = head;
for (int i = 0; n.next != null ; i++) {
n = n.next;
if (n.next.equals(t)){
return i;
}
}
return -1;
}
//获取第一个元素
public T getFirst(){
if (isEmpty()){
return null;
}
return head.next.item;
}
//获取最后一个元素
public T getLast(){
if (isEmpty()){
return null;
}
return last.item;
}
@Override
public Iterator<T> iterator() {
return new TIterator();
}
private class TIterator implements Iterator{
private Node n =head;
@Override
public boolean hasNext() {
return n.next != null;
}
@Override
public Object next() {
n = n.next;
return n.item;
}
}
}
Test类:
public class TwoWayLinkListTest {
public static void main(String[] args) throws Exception{
TwoWayLinkList<Integer> list = new TwoWayLinkList<>();
System.out.println(list.length());
list.insert(0,1);
list.insert(1,2);
list.insert(2,3);
list.insert(3,4);
list.insert(4,5);
//测试length方法
for (int i : list) {
System.out.println(i);
}
System.out.println(list.length());
System.out.println("-------------------");
//测试get方法
System.out.println(list.get(2));
System.out.println("------------------------");
//测试remove方法
int remove = list.remove(1);
System.out.println(remove);
System.out.println(list.length());
System.out.println("----------------");
for (int i : list) { System.out.println(i); }
}
}
0
1
2
3
4
5
5
---------------
3
---------------
2
4
---------------
1
3
4
5
链表的复杂度分析
- get(int i):每一次查询,都需要从链表的头部开始,依次向后查找,随着数据元素N的增多,比较的元素越多,时间复杂度为O(n)
- insert(int i,T t):每一次插入,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n);
- remove(int i):每一次移除,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n)
相比较顺序表,链表插入和删除的时间复杂度虽然一样,但仍然有很大的优势,因为链表的物理地址是不连续的,它不需要预先指定存储空间大小,或者在存储过程中涉及到扩容等操作,同时它并没有涉及的元素的交换。
相比较顺序表,链表的查询操作性能会比较低。因此,如果我们的程序中查询操作比较多,建议使用顺序表,增删操作比较多,建议使用链表。
链表反转
单链表的反转是非常经典的一个题目。需求非常明确:将链表中的元素按照相反的顺序排列。
- API设计:
- public void reverse():对整个链表反转 public Node
- reverse(Node curr):反转链表中的某个结点curr,并把反转后的curr结点返回
使用递归可以完成反转,递归反转其实就是从原链表的第一个存数据的结点开始,依次递归调用反转每一个结点,直到把最后一个结点反转完毕,整个链表就反转完毕。
- 代码实现
//单链表的反转
public void reverse(){
//判断当前链表是不是空链表,如果是则结束,如果不是则调用重载的reverse方法完成反转
if (isEmpty()){
return;
}
reverse(head.next);
}
//反转指定节点curr并返回指定节点
public Node reverse(Node curr){
if (curr.next==null){
head.next = curr;
return curr;
}
//递归地反转当前节点curr的下一个节点,返回值就是链表反转后当前节点的上一个节点
Node pre = reverse(curr.next);
//让返回的节点的下一个节点变为curr
pre.next = curr;
//把当前节点的下一个节点变为null
curr.next = null;
return curr;
}
Test:
public class ReverseLinkListTest {
public static void main(String[] args) throws Exception{
LinkList<Integer> list = new LinkList<>();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
for (Integer i : list){
System.out.println(i+' ');
}
System.out.println("--------------");
list.reverse();
for (Integer i : list){
System.out.println(i+' ');
}
}
}
33
34
35
36
37
--------------
37
36
35
34
33