ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

线性表(链表)

2022-07-23 15:34:44  阅读:99  来源: 互联网

标签:Node 结点 线性表 next 链表 null public


1.2线性表(链表)

之前我们已经使用顺序存储结构实现了线性表,我们会发现虽然顺序表的查询很快,时间复杂度为O(1),但是增删的 效率是比较低的,因为每一次增删操作都伴随着大量的数据元素移动。这个问题有没有解决方案呢?有,我们可以 使用另外一种存储结构实现线性表,链式存储结构。

链表是一种物理存储单元上非连续、非顺序的存储结构,其物理结构不能只管的表示数据元素的逻辑顺序,数据元 素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列的结点(链表中的每一个元素称为结点)组成, 结点可以在运行时动态生成。

那我们如何使用链表呢?按照面向对象的思想,我们可以设计一个类,来描述结点这个事物,用一个属性描述这个 结点存储的元素,用来另外一个属性描述这个结点的下一个结点。

结点API设计:

类名 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<Integer>(11, null);
		Node<Integer> second = new Node<Integer>(13, null);
		Node<Integer> third = new Node<Integer>(12, null);
		Node<Integer> fourth = new Node<Integer>(8, null);
		Node<Integer> fifth = new Node<Integer>(9, null);
    
		//生成链表
		first.next = second;
		second.next = third;
		third.next = fourth;
		fourth.next = fifth;
} 	

1.2.1 单向链表

单向链表是链表的一种,它由多个结点组成,每个结点都由一个数据域和一个指针域组成,数据域用来存储数据, 指针域用来指向其后继结点。链表的头结点的数据域不存储数据,指针域指向第一个真正存储数据的结点。

1.2.1.1 单向链表API设计

类名 LinkList
构造方法 LinkList() :创建LinkList对象
成员方法 1.public void clear() : 空置线性表
2.public boolean 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 : 记录链表长度

实现LinkList:

import java.util.Iterator;
public class LinkList<T> implements Iterable<T>{
    //记录头结点
    private Node head;
    //记录链表的长度
    private int N;

    @Override
    public Iterator<T> iterator() {
        return new LIterator();
    }

    //结点类
    private class Node {
        //存储数据
        T item;
        //下一个结点
        Node next;
        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
    public LinkList(){
        //初始化头结点
        this.head = new Node(null,null);
        //初始化元素个数
        this.N = 0;
    }
    //清空链表
    public void clear(){
        head.next = null;
        this.N = 0;
    }
    //获取链表的长度
    public int length() {
        return N;
    }
    //判断链表是否为空
    public boolean isEmpty(){
        return N == 0;
    }
    //获取指定位置i的元素
    public T get(int i) {
        //通过循环,从头节点开始往后找,依次找i次,就可以找到对应的元素
        Node n = head.next;
        for (int index = 0; index<i;index++){
            n = n.next;
        }
        return n.item;
    }
    //向链表中添加元素t
    public void insert(T t){
        //找到最后一个结点
        Node n = head;
        while (n.next !=null){
            n = n.next;
        }
        //创建新结点保存元素t
        Node newNode = new Node(t, null);
        //让当前最后一个结点指向新结点
        n.next = newNode;
        //元素个数加一
        N++;
    }
    //向指定位置i处,添加元素t
    public void insert(int i,T t){
        //找到i位置前一个结点
        Node pre = head;
        for (int index = 0; index <=i-1 ;index++){
            pre = pre.next;
        }
        //找到i位置的结点
        Node curr = pre.next;
        //创建新结点,并且新结点需要指向i位置的结点
        Node newNode = new Node(t, curr);
        //原来i位置的前一个结点指向新结点即可
        pre.next = newNode;
//        元素个数加一
        N++;
    }
    //删除指定位置i处的元素,并返回被删除的元素
    public T remove(int i){
        //找到i位置的前一个结点
        Node pre = head;
        for (int index = 0; index <=i-1 ;index++){
            pre = pre.next;
        }
        //要找到i位置的结点
        Node curr = pre.next;
        //找到i位置的下一个结点
        Node nextNode = curr.next;
        //前一个结点指向下一个结点
        pre.next = nextNode;
        N--;
        return curr.item;
    }
    //查找出元素t在链表中第一次出现的位置
    public int indexOf(T t){
        //从头节点开始依次找到每一个结点,取出item,和t比较,如果相同,就找到了
        Node n = head;
        for (int i = 0; n.next!=null; i++){
            n = n.next;
            if (n.item == t){
                return i;
            }
        }
        return -1;
    }
    private class LIterator implements Iterator{
        private Node n;
        public LIterator(){
            this.n = head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }
}

//测试代码

public class LinkListTest {
    public static void main(String[] args) throws Exception {
        LinkList<String> list = new LinkList<>();
        list.insert(0, "张三");
        list.insert(1, "李四");
        list.insert(2, "王五");
        list.insert(3, "赵六");
//测试length方法
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println(list.length());
        System.out.println("-------------------");
//测试get方法
        System.out.println(list.get(2));
        System.out.println("------------------------");
//测试remove方法
        String remove = list.remove(1);
        System.out.println(remove);
        System.out.println(list.length());
        System.out.println("----------------");
        ;
        for (String s : list) {
            System.out.println(s);
        }
    }
 }

1.2.2 双向链表

​ 双向链表也叫双向表,是链表的一种,它由多个结点组成,每个结点都由一个数据域和两个指针域组成,数据域用来存储数据,其中一个指针域用来指向其后继结点,另一个指针域用来指向前驱结点。链表的头结点的数据域不存储数据,指向前驱结点的指针域值为null,指向后继结点的指针域指向第一个真正存储数据的结点。

按照面向对象的思想,我们需要设计一个类,来描述结点这个事物。由于结点是属于链表的,所以我们把结点类作为链表类的一个内部类来实现

1.2.2.1 结点API设计

类名 Node
构造方法 Node(T t,Node pre,Node next): 创建Node对象
成员变量 T item:存储数据 Node next:指向下一个结点 Node pre:指向上一个结点

1.2.2.2 双向链表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:记录尾结点
3.private int N:记录链表的长度

双向链表代码实现

package cn.linner;

import java.util.Iterator;

public class TwoWayLinkList<T> implements Iterable<T>{
    //首结点
    private Node head;
    //最后一个结点
    private Node last;
    //链表的长度
    private int N;



    private class Node {
        public Node(T item, Node pre, Node next) {
            this.item = item;
            this.pre = pre;
            this.next = next;
        }

        //存储数据
        public T item;
        //指向上一个结点
        public Node pre;
        //指向下一个结点
        public Node next;
    }

    public TwoWayLinkList() {
        //初始化头节点和为节点
        this.head = new Node(null, null, null);
        this.last = null;
        //初始化元素个数
        this.N = 0;
    }

    //清空链表
    public void clear() {
        this.head.next = null;
        this.head.pre = null;
        this.head.item = null;
        this.last = null;
        this.N = 0;
    }
    //获取链表长度
    public int length(){
        return N;
    }
    //判断链表是否为空
    public boolean isEmpty(){
        return N == 0;
    }
    //获取第一个元素
    public T getFirst(){
        if (isEmpty()){
            return null;
        }
        return head.next.item;
    }
    //获取最后一个元素
    public T getList(){
        if (isEmpty()){
            return null;
        }
        return last.item;
    }
    //插入元素
    public void insert(T t){
        //如果链表为空:
        if (isEmpty()) {
            //创建新结点
            Node newNode = new Node(t, head, null);
            //让新节点成为尾结点
            last = newNode;
            //让头节点指向尾结点
            head.next = last;
        }else {
            //如果链表不为空
            Node oldList = last;

            //创建新结点
            Node newNode = new Node(t, oldList, null);
            //让当前的尾结点指向新结点
            oldList.next = newNode;
            //让新结点成为尾结点
            last = newNode;
        }
//        元素个数加一
        N++;
    }
//          指定位置i插入一个元素
    public void insert(int i,T t){
        //找到i位置的前一个结点
        Node pre = head;
        for (int index = 0; index < i; index++){
            pre = pre.next;
        }
        //找到i位置结点
        Node curr = pre.next;
        //创建新结点
        Node newNode = new Node(t, pre, curr);
        //让i位置的前一个结点的下一个结点变为新结点
        pre.next = newNode;
        //让i位置的前一个结点变为新结点
        curr.pre = newNode;
        //元素个数加一
        N++;
    }

    //获取指导i位置的元素
    public T get(int i){
        Node n = head.next;
        for (int index = 0; index < i; index++){
            n = n.next;
        }
        return n.item;
    }
    //找到元素t在链表中第一次出现的位置
    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;
    }
    //删除位置i的元素,并返回该元素
    public T remove(int i){
        //找到i位置的前一个结点
        Node pre = head;
        for (int index = 0; index < i; index++){
            pre = pre.next;
        }
        //找到i位置的结点
        Node curr = pre.next;
        //找到i位置的下一个结点
        Node nextNode = curr.next;
        //让i位置的前一个结点的下一个结点变为i位置的下一个结点
        pre.next = nextNode;
        //让i位置的下一个结点的上一个结点,变为i位置的前一个结点
        nextNode.pre = pre;
        //元素个数减一
        N--;
        return curr.item;
    }
    @Override
    public Iterator<T> iterator() {
        return new TIterator();
    }
    private class TIterator implements Iterator{
        private Node n;
        public TIterator(){
            this.n = head;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }
}

测试类

package cn.linner;

public class TwoWayLinkListTest {
    public static void main(String[] args) {
        TwoWayLinkList<Object> sl = new TwoWayLinkList<>();
        sl.insert("张三");
        sl.insert("张4");
        sl.insert("张5");
        sl.insert("张6");
        for (Object o : sl) {
            System.out.println(o);
        }
        System.out.println("---------------------------------");
        //测试获取
        Object o = sl.get(1);
        System.out.println("索引1处的纸为"+o);
        //测试第一个和最后一个元素的获取
        System.out.println("第一个元素为"+ sl.getFirst());
        System.out.println("最后一个元素为"+ sl.getList());
        //测试删除
        Object remove = sl.remove(2);
        System.out.println("删除的元素为"+remove);
        //测试清空
        sl.clear();
    }
}

1.2.2.4 java中LinkedList实现

java中LinkedList集合也是使用双向链表实现,并提供了增删改查等相关方法

1.底层是否用双向链表实现;

2.结点类是否有三个域

1.2.3 链表的复杂度分析

get(int i):每一次查询,都需要从链表的头部开始,依次向后查找,随着数据元素N的增多,比较的元素越多,时间复杂度为O(n)

insert(int i,T t):每一次插入,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n);

remove(int i):每一次移除,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n)

相比较顺序表,链表插入和删除的时间复杂度虽然一样,但仍然有很大的优势,因为链表的物理地址是不连续的, 它不需要预先指定存储空间大小,或者在存储过程中涉及到扩容等操作,,同时它并没有涉及的元素的交换。

相比较顺序表,链表的查询操作性能会比较低。因此,如果我们的程序中查询操作比较多,建议使用顺序表,增删 操作比较多,建议使用链表。

1.2.4链表反转

单链表的反转,是面试中的一个高频题目。

需求:

原链表中数据为:1->2->3>4

反转后链表中数据为:4->3->2->1

反转API:

public void reverse():对整个链表反转

public Node reverse(Node curr):反转链表中的某个结点curr,并把反转后的curr结点返回

使用递归可以完成反转,递归反转其实就是从原链表的第一个存数据的结点开始,依次递归调用反转每一个结点, 直到把最后一个结点反转完毕,整个链表就反转完毕。

代码:

//反转整个链表
public void reverse(){
    //判断当前链表是否为空链表,如果是空链表,则结束运行,如果不是,则需要调用重载的reverse方法完成反转
    if (isEmpty()){
        return;
    }
    reverse(head.next);
}

//反转指定的结点,并把反转后的结点返回
public Node reverse(Node curr){
    if (curr.next == null){
        head.next = curr;
        return curr;
    }
    //递归的反转当前结点的下一个结点;返回值就是链表反转后,当前结点的上一个结点
    Node pre = reverse(curr.next);
    //让返回的结点的下一个结点变为当前结点
    pre.next = curr;
    //把当前结点的下一个结点变为null
    curr.next = null;

    return curr;
}

1.2.5快慢指针

​ 快慢指针指的是定义两个指针,这两个指针的移动速度一块一慢,以此来制造出自己想要的差值,这个差值可以然我们找到链表上相应的结点。一般情况下,快指针的移动步长为慢指针的两倍

1.2.5.1 中间值问题

我们先来看下面一段代码,然后完成需求。

//测试类
public class Test {
public static void main(String[] args) throws Exception {
		Node<String> first = new Node<String>("aa", null);
		Node<String> second = new Node<String>("bb", null);
		Node<String> third = new Node<String>("cc", null);
		Node<String> fourth = new Node<String>("dd", null);
		Node<String> fifth = new Node<String>("ee", null);
		Node<String> six = new Node<String>("ff", null);
		Node<String> seven = new Node<String>("gg", null);
//完成结点之间的指向
   		    first.next = second;
			second.next = third;
			third.next = fourth;
			fourth.next = fifth;
			fifth.next = six;
			six.next = seven;
//查找中间值
		String mid = getMid(first);
		System.out.println("中间值为:"+mid);
}
/**
* @param first 链表的首结点
* @return 链表的中间结点的值
*/
public static String getMid(Node<String> first) {
		return null;
}
//结点类
private static class Node<T> {
//存储数据
T item;
//下一个结点
Node next;
public Node(T item, Node next) {
		this.item = item;
		this.next = next;
			}
		}
}

需求:

​ 请完善测试类Test中的getMid方法,可以找出链表的中间元素值并返回。 利用快慢指针,我们把一个链表看成一个跑道,假设a的速度是b的两倍,那么当a跑完全程后,b刚好跑一半,以 此来达到找到中间节点的目的。 如下图,最开始,slow与fast指针都指向链表第一个节点,然后slow每次移动一个指针,fast每次移动两个指针。

代码:

/**
* @param first 链表的首结点
* @return 链表的中间结点的值
*/
public static String getMid(Node<String> first) {
		Node<String> slow = first;
		Node<String> fast = first;
	while(fast!=null && fast.next!=null){
		fast=fast.next.next;
		slow=slow.next;
	}
	return slow.item;
}
1.2.5.2 单向链表是否有环问题

看下面代码,完成需求:

//测试类
public class Test {
	public static void main(String[] args) throws Exception {
		Node<String> first = new Node<String>("aa", null);
		Node<String> second = new Node<String>("bb", null);
		Node<String> third = new Node<String>("cc", null);
		Node<String> fourth = new Node<String>("dd", null);
		Node<String> fifth = new Node<String>("ee", null);
		Node<String> six = new Node<String>("ff", null);
		Node<String> seven = new Node<String>("gg", null);
//完成结点之间的指向
		first.next = second;
		second.next = third;
		third.next = fourth;
		fourth.next = fifth;
		fifth.next = six;
		six.next = seven;
//产生环
		seven.next = third;
//判断链表是否有环
		boolean circle = isCircle(first);
        System.out.println("first链表中是否有环:"+circle);
	}
/**
* 判断链表中是否有环
* @param first 链表首结点
* @return ture为有环,false为无环
*/
	public static boolean isCircle(Node<String> first) {
		return false;
}
//结点类
	private static class Node<T> {
//存储数据
		T item;
//下一个结点
		Node next;
	public Node(T item, Node next) {
		this.item = item;
		this.next = next;
		}
	}
}

需求:

请完善测试类Test中的isCircle方法,返回链表中是否有环。 使用快慢指针的思想,还是把链表比作一条跑道,链表中有环,那么这条跑道就是一条圆环跑道,在一条圆环跑道中,两个人有速度差,那么迟早两个人会相遇,只要相遇那么就说明有环。

代码:

/**
* 判断链表中是否有环
* @param first 链表首结点
* @return ture为有环,false为无环
*/
public static boolean isCircle(Node<String> first) {
		Node<String> slow = first;
		Node<String> fast = first;
	while(fast!=null && fast.next!=null){
		fast = fast.next.next;
		slow = slow.next;
    	if (fast.equals(slow)){
			return true;
}
}
return false;
}
1.2.5.3有环链表找环入口

原理图:

当快捷点和慢结点相遇的时候,在开始的头节点设置一个和慢指针一样的新指针,当慢指针与开头设置的新指针相遇的时候说明当前结点为环形链表的入口结点。

public static Node getEntrance(Node<String> first) {
		Node<String> slow = first;
		Node<String> fast = first;
		Node<String> temp = null;
	while(fast!=null && fast.next!=null){
		fast = fast.next.next;
		slow=slow.next;
		if (fast.equals(slow)){
			temp = first;
			continue;
		}
		if (temp!=null){
		temp=temp.next;
		if (temp.equals(slow)){
			return temp;
			}
		}
	}
		return null;
}

标签:Node,结点,线性表,next,链表,null,public
来源: https://www.cnblogs.com/wakanda-forever/p/16512131.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有