Java实现单链表

一、什么是单链表

单链表是链表的其中一种基本结构。在结点中数据域用来存储数据元素,指针域用于指向下一个具有相同结构的结点。
因为只有一个指针结点,称为单链表。

二、单链表的结构

在这里插入图片描述

三、单链表的基本操作

1.创建类和构造方法

class Node{
    public int val;
    public Node next;
    public Node(){

    }
    public Node(int val){
        this.val = val;
    }
}

2.建立单链表

public class MyLinkedList {
    public Node head;
    public void createLinkedList(){
        this.head = new Node(2);
        Node node2 = new Node(3);
        Node node3 = new Node(4);
        Node node4 = new Node(5);
        Node node5 = new Node(6);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;
    }
}

3.遍历单链表

   public void traverse(){
        Node cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

4.找到单链表的最后一个结点

   public Node findLastNode(){
        Node cur = head;
        if(cur == null){
            return null;
        }
        while(cur.next != null){
            cur = cur.next;
        }
        return cur;
    }

5.找到倒数第二个结点`

    public Node findLastTwoNode(){
        Node cur = head;
        if(cur == null){
            return null;
        }
        if(cur.next == null){
            return null;
        }
        while(cur.next.next != null){
            cur = cur.next;
        }
        return cur;
    }

6.求链表的长度

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

7.求链表的第N个结点

    public Node findN(int n){
        if(head == null){
            System.out.println("单链表为空!");
            return null;
        }
        if(n <= 0){
            System.out.println("n不合法!");
        }
        if(n > size()){
            System.out.println("n不合法!");
        }
        int count = 1;
        Node cur = head;
        while(count != n){
            cur = cur.next;
            count++;
        }
        return cur;
    }

8.查找关键字key是否在单链表中

    public boolean findKey(int key){
        Node cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

9.头插

    public void addFirst(int data){
        Node node = new Node(data);
        node.next = head;
        head = node;
    }

10.尾插

    public void addLast(int data){
        Node node = new Node(data);
        Node cur = head;
        if(cur == null){
            head = node;
        }

        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

11.任意位置的插入

    public void addIndex(int index,int data){
        if(index < 0 || index > size()){
            System.out.println("插入位置不合法!");
        }
        //头插
        if(index == 0){
            addFirst(data);
            return;
        }
        //尾插
        if(index == size()){
            addLast(data);
            return;
        }
        //中间插
        Node node = new Node(data);
        Node cur = head;
        int count = 0;
        while(count != index-1){
            count++;
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

12.找到关键字为key的前一结点

    public  Node searchPre(int key){
        Node cur = head;
        while(cur.next != null){
            if(cur.next.val == key){
                return  cur;
            }
            cur = cur.next;
        }
        return null;
    }

13.删除关键字第一次为key的结点

    public void deleteKey(int key){
        if(head == null){
            return;
        }
        if(head.val == key){
            head = head.next;
            return;
        }
        Node prev = searchPre(key);
        if(prev == null){
            System.out.println("不存在关键字为key的结点");
        }else{
            prev.next = prev.next.next;
        }
    }

14.删除index位置的结点

   public void delete(int index){
        if(index < 0 || index >= size()){
            System.out.println("删除位置不合法!");
        }
        if(index == 0){
            head = head.next;
            return;
        }
        Node cur = head;
        int count = 0;
        while(count != index-1){
            cur = cur.next;
            count++;
        }
        cur.next = cur.next.next;
    }

15.清空单链表

    public void clear(){
        head = null;
    }
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值