一文让你掌握数据结构之---链表

引言

很多情况下,我们都是使用数组做为数据的存储结构的,但是熟悉编程的人都了解,数组在一定的情况下存在一定的缺陷,比如在无须的数组中,其搜索效率非常低,而且数组一旦确定,其大小便不能在修改,因此有必要引入一种新的数据结构来解决部分上述的问题。这里讲解的便是链表,除数组之外另外一种常用的数据结构。

链接点

这部分简单的介绍下链表的基本结构,一个链接点是某个类的对象,这个类定义为Link,因为一个链表中有很多的链接点。其下一个节点可以用next来表示。其基本的关系图如下
链表

可用如下程序来表述一个链表

public class Link {

    public int idata;
    public double ddata;
    public  Link next;

    //
    public Link(int d1 , double d2){
        idata = d1;
        ddata = d2;
    }
    //display
    public void display(){
        System.out.println("{  "+idata+"  ,  "+ddata+"   }");
    }
}

它包含了一些数据和对下一个链接点的引用。这种类定义叫做自引式,这个可以理解为其包含了一个和自己相同的字段。

单链表

单链表的比较好理解,可以使用仅有的3项操作来实现简单的单链表。
- 在链表头插入一个数据
- 在链表头删除一个数据
- 遍历链表
结合上述的Link程序。可以构造一个简单的单链表

package link_test;

public class LinkList {
    private Link first;
    //
    public LinkList(){
        first = null;
    }
    //isEmpty
    public boolean  isEmpty(){
        return  (first == null);
    }
    //insert
    public void insertFirst(int id ,double dd){
        Link newlink = new Link(id , dd);
        newlink.next = first;    //old first give next
        first = newlink;
    }
    //deleteFirst
    public Link deleteFirst(){
        Link temp = first;
        first  = first.next;
        return temp;
    }
    //display
    public  void disPlayList(){
        System.out.println("linklist (first--->last)");
        Link current = first;
        while(current != null){
            current.display();
            current = current.next;
        }
    }
    //find by key
    public Link find(int key){
        Link current = first;
        while(current.idata != key){
            if(current.next ==null)
                return null;
            else
                current = current.next;
        }
        return current;
    }

    //delet by key 
    public Link delet(int key){
        Link current = first;
        Link previous = first;
        while(current.idata != key){
            if(current.next ==null)
                return null;
            else{
                previous = current;
                current = current.next;
            }   
        }
        if(current ==first)
            first = first.next;
        else
            previous.next = current.next;
        return current;
    }   
}

从上述的代码中可以看出,链表其实与第一个节点有着密切的关系,遍历一个链表需要一致next下去,寻找一个节点也需要一直的next寻址,其效率也不是很高,但是其删除操作却不需要删除之后后面的所有数据都进行移动,因为其只需要将删除的位置进行断开,将next接入下一个点即可。引入一张百度图片的火车图来说明下删除节点。
这里写图片描述
是不是很形象的说明了删除只需要断开即可。可结合程序进行了解。

public Link deleteFirst(){
        Link temp = first;
        first  = first.next;
        return temp;
    }

双端链表

双端链表与传统的链表非常相似,但是其有一个新的特性:可以对最后一个链接点进行引用。可用下图来表述
这里写图片描述
对最后一个链接点的引用像表头一样,在表尾直接的插入一个链接点。在单链表中若想在表尾插入一个节点,必须从first遍历到last。这样的话其效率非常的低。这里有一点需要注意,必须和双向链表进行区分,双向链表等会介绍。但是双端链表对最后一个节点进行插入操作还是有一点的难度。这里给出按照key的顺序进行插入和查找的java代码。

public void  insertBykey(int key){
        Link newLink = new Link(key);
        Link pervious = null;
        Link current = first;

        while(current!=null && key>current.data){
            pervious = current;
            current = current.next;
        }
        if(current == null)
            first = newLink;
        else
            pervious.next = newLink;
        newLink.next = current;
    }

链表的效率

在白头插入和删除其速度开所花费O(1)的时间。平均来说,查找、删除在指定的位置插入都需要搜索一般的链表 1+n2 ,同样在数组中也需要O(N)次的比较,但是链表还是要快点。因为在插入和删除的同时链表不需要移动其他数据。

双向链表

双向连链表可用下图表述。
这里写图片描述
双向链表可以向前遍历,可以向后遍历,因为在每个链接点都有两个链接点的引用,在双向链表中这样定义Link

class Link{
    public long dData;
    public Link next;
    public Link previous;
}

与前面的不同的是双向链表在处理每次操作中需要处理4个节点的引用,而不是两个。当然链接点所占的空间也变大了一点。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值