数据结构链表-java

一.单链表的介绍

1.什么是链表

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
在这里插入图片描述
在这里插入图片描述
从上图可以看到链表在逻辑上是连续的但是在物理上是不连续的。节点一般是从堆上申请出来的。

2.链表和顺序表的区别

顺序表优点:根据指定的下标去索引,效率非常高,更新元素也很快,时间复杂度为o(1)。
顺序表缺点:1.每次插入或者删除都需要移动元素,最坏情况下时间复杂度为0(N)。
2 .当顺序表满了以后需要扩容,扩容为1,5倍扩容,如果只放一个元素对空间极大的浪费。
总结顺序表用于查找元素或者经常更新元素使用。
链表:1.链表没有容量的概念不需要扩容
2,随机访问元素时间复杂度为o(n)。
3.头插,头删只需要改变引用时间复杂度为o(1)。
总结:l链表用于频繁插入和删除元素的场景.

二.单链表的模拟实现

1.单链表的定义

class SeqListNode{
   public  class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }

    }
    public ListNode head;

2.单链表的头插

 public void ListNodePushFront(int val){
       ListNode node = new ListNode(val);
       node.next = head;
       head = node;
   }

单链表的头插从一个空表开始,将读取到的数据存放到新节点的数据域中,然后将新节点插入到当前链表的表头。
在这里插入图片描述

3.单链表尾插

public void ListNodeBack(int val){
       ListNode node = new ListNode(val);
       if(head==null){
           head = node;
       }
       ListNode cur = head;
       while(cur.next!=null){
           cur = cur.next;
       }
       cur.next = node;

   }

单链表的尾插首先定义一个指向去寻找单链表的尾巴,不能改变head 的指向,所以定义一个cur,找到单链表的尾巴,改变最后一个节点的next域,使其指向新的节点。时间复杂度为O(N)。
在这里插入图片描述

4.单链表任意位置插入

 public void ListNodePush(int val,int pos){

       ListNode node = new ListNode(val);
       ListNode cur = head;
       while(--pos>0){
           cur = cur.next;
       }
      // cur.next = node;
       node.next = cur.next;
       cur.next = node;
   }

单链表任意位置插入节点的时候不能先cur.next = node,这样会改变cur节点next域的指向,使其找不到下一个节点的位置。先让节点的next域指向cur的下一个节点,再让cur指向node节点。最坏情况是尾插,最后情况是头插。最坏时间复杂度为o(n)。
在这里插入图片描述

5.单链表头删

 public void ListNodePopFront(){
       if(head==null){
           return;
       }
       head = head.next;
   }

头删让头节点指向下一个节点的位置。头删的时间复杂度为o(1)。
在这里插入图片描述

6.单链表的尾删除

 public void ListNodePopBack(){
       if(head==null){
           return;
       }
       ListNode cur = head;
       ListNode prev = null;
       while(cur.next!=null){
           prev = cur;
           cur= cur.next;
       }
       prev.next = null;
   }

让cur找最后一个节点,prev记录最后一个节点的前驱,找到最后一个节点,使其前驱的Next域置空。时间复杂度为o(n)。
在这里插入图片描述

7.单链表任意位置删除

public void ListNodePop(int pos){
       if(head==null){
           return;
       }
       if(head.next==null){
           return;
       }
       ListNode cur = head;
       ListNode prev = null;
       while(--pos>0){
           prev = cur;
           cur=cur.next;
       }
       prev.next= cur.next;

   }

在这里插入图片描述

8.单链表求长度

public int size(){
       int cout = 0;
       ListNode cur = head;
       while(cur!=null){
           cur = cur.next;
           cout++;
       }
       return cout;
      }

三.单链表模拟实现总代码

class SeqListNode{
   public  class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }

    }
    public ListNode head;
   public void ListNodePushFront(int val){
       ListNode node = new ListNode(val);
       node.next = head;
       head = node;
   }
   public void ListNodeShow(){
       ListNode cur = head;
       while(cur!=null){
           System.out.println(cur.val);
           cur = cur.next;
       }
   }
   public void ListNodeBack(int val){
       ListNode node = new ListNode(val);
       if(head==null){
           head = node;
       }
       ListNode cur = head;
       while(cur.next!=null){
           cur = cur.next;
       }
       cur.next = node;

   }
   public void ListNodePopFront(){
       if(head==null){
           return;
       }
       head = head.next;
   }
   public void ListNodePopBack(){
       if(head==null){
           return;
       }
       ListNode cur = head;
       ListNode prev = null;
       while(cur.next!=null){
           prev = cur;
           cur= cur.next;
       }
       prev.next = null;
   }
   public void ListNodePush(int val,int pos){

       ListNode node = new ListNode(val);
       ListNode cur = head;
       while(--pos>0){
           cur = cur.next;
       }
      // cur.next = node;
       node.next = cur.next;
       cur.next = node;
   }
   public void ListNodePop(int pos){
       if(head==null){
           return;
       }
       if(head.next==null){
           return;
       }
       ListNode cur = head;
       ListNode prev = null;
       while(--pos>0){
           prev = cur;
           cur=cur.next;
       }
       prev.next= cur.next;

   }
      public int size(){
       int cout = 0;
       ListNode cur = head;
       while(cur!=null){
           cur = cur.next;
           cout++;
       }
       return cout;
      }
}
public class Test {
    public static void main(String[] args) {
        SeqListNode seqlist1 = new SeqListNode();
        seqlist1.ListNodePushFront(1);
        seqlist1.ListNodePushFront(2);
        seqlist1.ListNodePushFront(3);
        seqlist1.ListNodePushFront(4);
        seqlist1.ListNodePushFront(11);
        seqlist1.ListNodeBack(7);
        seqlist1.ListNodeShow();
        seqlist1.ListNodePopFront();
        System.out.println("****************");
        seqlist1.ListNodeShow();
        System.out.println("***************");
        seqlist1.ListNodePopBack();
        seqlist1.ListNodeShow();
        System.out.println("***********************");
        seqlist1.ListNodePush(24,1);
        seqlist1.ListNodeShow();
        System.out.println("**************************");
        seqlist1.ListNodePop(3);
        seqlist1.ListNodeShow();
        int ret = seqlist1.size();
        System.out.println("***************************");
        System.out.println(ret);
    }
}

三.双向循环链表

一.双向循环链表的介绍

单链表只有一个指向其后续的域,使得单链表只能从前到后依次遍历,要访问某个结点的前驱结点(插入、删除操作时),只能从头开始遍历,访问后继结点的时间复杂度为O(1),访问前驱结点的时间复杂度为0(n)。
为了一服单链表的上述缺点,引入了双链表,双链表结点中有两个域prev和next域,分别别指向其前驱结点和后继结点。
在这里插入图片描述
双链表在单链表的结点中增加了一个指向其前驱prev,因此双链表中的按值查找和按位查找的操作与单链表的相同。但双链表在插入和删除操作的实现上,与单链表有着较大的不同。这是因为“链”变化时也需要对prev 域做出修改,其关键是保证在修改的过程中不断链。此外,双链表可以很方便地找到其前驱结点,因此,插入、删除操作的时间复杂度仅为0(1)。

二.双向链表的模拟实现

1.双向链表的定义

class MyNode{
    class SListNode{
        int val;
        SListNode prev;
        SListNode next;
        public SListNode(int val){
            this.val = val;
        }

    }
    public SListNode head;
    public SListNode last;

2.双向链表的头插

 public void addFirst(int val){
        SListNode node = new SListNode(val);
        if(head==null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

在这里插入图片描述
双向链表的头插首先要开辟一个新的节点,把新结点的prev域置空,让其next的域指向原来链表的头节点,其旧节点的prev域指向新的节点,头节点的指向变为新的节点node。

3.双向链表的尾插

public void addBack(int val){

        SListNode node  = new SListNode(val);
        if(head==null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = last.next;

    }

在这里插入图片描述

对比单链表双向链表的优势体现了出来,尾插的时候last已经指向了链表的尾部,把时间复杂度提升到了o(1)。

4.双向链表任意位置插入

public void addIndex(int val,int pos){
        if(pos<=0&&pos>size()){
            return;
        }
        SListNode node = new SListNode(val);
        SListNode cur = head;
        while(--pos>0){
            cur = cur.next;
        }
         node.next = cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
    }

在这里插入图片描述

双向链表在任意位置插入元素时,如果是在头部插入直接调用头插函数,如果是尾部插入直接调用尾插,进行插入的时候从后往前改指向,从前往后改指向一般会找不到下一个元素的位置,导致之后的操作存在问题。时间复杂度为o(n)。

5.双向链表头删

 public void  popFront(){
        if(head == null){
            return;
        }
        head = head.next;
        head.prev = null;
    }

在这里插入图片描述

6.双向链表的尾删

  public void popBack(){
        if(size()==1){
            popFront();
        }
        if(head==null){
            return;
        }
        SListNode cur = last.prev;
        last.prev.next=null;
        last = cur;

    }

7.双向链表的任意位置删除

public void popIndex(int pos){
        if(head == null){
            return;
        }
        if(pos<=0&&pos>size()){
            return;
        }
        if(pos==0){
            popFront();
        }
        if(pos==size()){
            popBack();
        }
        SListNode cur = head;
        while(--pos>0){
           cur = cur.next;
        }
        cur.next.prev= cur.prev;
        cur.prev.next = cur.next;
    }

在这里插入图片描述
循环链表和单链表的插入删除操作几乎是一样的,所不同的是若操作在尾部,单链表需要找尾巴,时间复杂度为o(n),而双向链表的last记录了最后一个节点的位置,可以直接进行插入和删除操作,其时间复杂度为o(1)。

8.双向链表的长度

  public int size(){
        int count = 0;
        if(head ==null){
            return 0;
        }
        SListNode cur = head;
        while(cur!=null){
            cur = cur.next;
            count++;
        }
        return count;
    }

三.双向链表模拟实现总代码

class MyNode{
    class SListNode{
        int val;
        SListNode prev;
        SListNode next;
        public SListNode(int val){
            this.val = val;
        }

    }
    public SListNode head;
    public SListNode last;
    public void addFirst(int val){
        SListNode node = new SListNode(val);
        if(head==null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    public void addBack(int val){

        SListNode node  = new SListNode(val);
        if(head==null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = last.next;

    }
    public void show(){
        SListNode cur = head;
        while(cur!=null){
            System.out.print(" "+cur.val);
            cur = cur.next;
        }
        System.out.println();
    }
    public void addIndex(int val,int pos){
        if(pos<=0&&pos>size()){
            return;
        }
        SListNode node = new SListNode(val);
        SListNode cur = head;
        while(--pos>0){
            cur = cur.next;
        }
         node.next = cur;
        cur.prev.next=node;
        node.prev=cur.prev;
        cur.prev=node;
    }
    public void  popFront(){
        if(head == null){
            return;
        }
        head = head.next;
        head.prev = null;
    }
    public void popBack(){
        if(size()==1){
            popFront();
        }
        if(head==null){
            return;
        }
        SListNode cur = last.prev;
        last.prev.next=null;
        last = cur;

    }
    public void popIndex(int pos){
        if(head == null){
            return;
        }
        if(pos<=0&&pos>size()){
            return;
        }
        if(pos==0){
            popFront();
        }
        if(pos==size()){
            popBack();
        }
        SListNode cur = head;
        while(--pos>0){
           cur = cur.next;
        }
        cur.next.prev= cur.prev;
        cur.prev.next = cur.next;
    }
    public int size(){
        int count = 0;
        if(head ==null){
            return 0;
        }
        SListNode cur = head;
        while(cur!=null){
            cur = cur.next;
            count++;
        }
        return count;
    }
}
public class Test {
    public static void main(String[] args) {
        MyNode mynode = new MyNode();
        mynode.addFirst(1);
        mynode.show();
        mynode.addBack(2);
        mynode.addBack(2);
        mynode.addBack(2);
        mynode.addBack(2);

        mynode.show();
        mynode.addIndex(11,3);
        mynode.show();
        mynode.popFront();
        mynode.show();
        mynode.popBack();
        mynode.show();
        mynode.popIndex(2);
        mynode.show();
        int ret=mynode.size();
        System.out.println(ret);
    }
}

四.运行结果

在这里插入图片描述

四.LinkedList

1.linkedList的介绍

在这里插入图片描述

LinkedList的底层是双向链表结构(链表后面介绍),由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。

2.LinkedList的特点

  1. LinkedList实现了List接口
  2. LinkedList的底层使用了双向链表
  3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
  4. LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
  5. LinkedList比较适合任意位置插入的场景

3.LinkedList的构造

在这里插入图片描述

public static void main(String[] args) {
// 构造一个空的LinkedList
List<Integer> list1 = new LinkedList<>();
List<String> list2 = new java.util.ArrayList<>();
list2.add("JavaSE");
list2.add("JavaWeb");
list2.add("JavaEE");
// 使用ArrayList构造LinkedList
List<String> list3 = new LinkedList<>(list2);
}

4.Linked其他方法介绍

在这里插入图片描述

public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(1); // add(elem): 表示尾插
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println(list.size());
System.out.println(list);
// 在起始位置插入0
list.add(0, 0); // add(index, elem): 在index位置插入元素elem
System.out.println(list);
list.remove(); // remove(): 删除第一个元素,内部调用的是removeFirst()
list.removeFirst(); // removeFirst(): 删除第一个元素
list.removeLast(); // removeLast(): 删除最后元素
list.remove(1); // remove(index): 删除index位置的元素
System.out.println(list);
// contains(elem): 检测elem元素是否存在,如果存在返回true,否则返回false
if(!list.contains(1)){
list.add(0, 1);
}
list.add(1);
System.out.println(list);
System.out.println(list.indexOf(1)); // indexOf(elem): 从前往后找到第一个elem的位置
System.out.println(list.lastIndexOf(1)); // lastIndexOf(elem): 从后往前找第一个1的位置
int elem = list.get(0); // get(index): 获取指定位置元素
list.set(0, 100); // set(index, elem): 将index位置的元素设置为elem
System.out.println(list);
// subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
List<Integer> copy = list.subList(0, 3);
System.out.println(list);
System.out.println(copy);
list.clear(); // 将list中元素清空
System.out.println(list.size());
}

编译器上运行效果

 LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);
        System.out.println(list.size());
        list.add(1,11);
        System.out.println(list);
        list.remove();
        System.out.println(list);
        list.remove(3);
        System.out.println(list);
        System.out.println(list.get(2));
        list.set(0,100);
        System.out.println(list);
        System.out.println(list.lastIndexOf(3));
    }
}

在这里插入图片描述

5.linkedlist的打印

public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(1); // add(elem): 表示尾插
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println(list.size());
// foreach遍历
for (int e:list) {
System.out.print(e + " ");
} S
ystem.out.println();
// 使用迭代器遍历---正向遍历
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
System.out.print(it.next()+ " ");
} S
ystem.out.println();
// 使用反向迭代器---反向遍历
ListIterator<Integer> rit = list.listIterator(list.size());
while (rit.hasPrevious()){
System.out.print(rit.previous() +" ");
} S
ystem.out.println();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值