【JAVA】实现一个无头单向非循环链表

【JAVA】实现一个无头单向非循环链表

链表

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
链表的结构多样,这里列出8种链表结构:

单向带头循环、单向不带头循环、单向带头非循环、单向不带头非循环
双向带头循环、双向不带头循环、双向带头非循环、双向不带头非循环

虽然有这么多的链表的结构,但是重点掌握两种:
无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。
无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

无头单向非循环链表实现

MyLinkedList.java

class   Node{
    public int val;
    public Node next;
    public Node(){
    }
    public Node(int val) {
        this.val = val;
    }
}
//无头单向非循环链表实现
public class MyLinkedList {
    public Node head;//表示当前链表的头,默认null
    //构建链表
    public void createdLinked() {
        this.head = new Node(12);
        Node node2 = new Node(22);
        Node node3 = new Node(32);
        Node node4 = new Node(42);
        this.head.next = node2;
        node2.next = node3;
        node3.next = node4;
    }
    //得到单链表的长度
    public int size() {
        Node cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //清空链表
    public void clear() {
        this.head = null;
    }
    //打印链表
    public void display() {
        Node cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display1(Node node){
        while(node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }
    //找链表最后一个节点
    public Node findLastNode() {
        if(this.head == null) {
            System.out.println("head == null");
            return null;
        }
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        return cur;
    }
    //找链表倒数第二个节点
    public Node findLastTwoNode() {
        if(this.head == null) {
            System.out.println("head == null");
            return null;
        }
        if(this.head.next == null) {
            System.out.println("Only One Node");
            return null;
        }
        Node cur = this.head;
        while(cur.next.next != null) {
            cur = cur.next;
        }
        return cur;
    }
    //找到第n个节点
    public Node findN(int n){
        if(this.head == null) {
            System.out.println("head == null");
            return null;
        }
        if(n <= 0) {
            System.out.println("太小");
            return null;
        }
        if(n > size()){
            System.out.println("太大");
            return null;
        }
        int count = 0;
        Node cur = this.head;
        while(count != n) {
            count++;
            cur = cur.next;
        }
        return cur;
    }
    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
        }else{
            node.next = this.head;
            this.head = node;
        }
    }
    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if(this.head == null) {
            this.head = node;
        }else{
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }
    //找到index-1位置的节点的引用
    public Node moveIndex(int index) {
        Node cur = this.head;
        int count = 0;
        while(count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        if(index < 0 || index > size()) {
            System.out.println("illegal");
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        Node cur = moveIndex(index);//cur保存index - 1位置的节点的引用
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //查找key的前驱,如果有返回前驱节点的引用,没有返回null
    public Node searchPrev(int key) {
        Node cur = this.head;
        while (cur.next != null) {
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head == null) {
            System.out.println("head == null");
        }
        if(this.head.val == key){
            this.head = this.head.next;
        }
        Node prev = searchPrev(key);
        if(prev == null) {
            System.out.println("error");
        }else{
            Node del = prev.next;
            prev.next = del.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllkey(int key) {
        if(this.head == null){
            System.out.println("head == null");
        }
        Node prev = this.head;
        Node cur = prev.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            }else{
                prev = cur;
            }
            cur = cur.next;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }
    //反转链表
    public Node reverseList() {
        Node cur = this.head;
        Node prev = null;
        Node newHead = null;
        while (cur != null) {
            Node curNext = cur.next;
            if(curNext == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return newHead;
    }
    //返回链表的中间节点,如果有两个中间节点则返回第二个
    public Node middleNode1(){
        int len = size() / 2;
        Node cur = this.head;
        int count = 0;
        while (count != len) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    public Node middleNode() {
        Node fast = this.head;
        Node slow = this.head;
        while (fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

Main.java

public class Main {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.createdLinked();//12 22 32 42
        myLinkedList.addLast(100);//12 22 32 42 100
        myLinkedList.addFirst(1);//1 12 22 32 42 100
        myLinkedList.addIndex(0,110);//110 1 12 22 32 42 100
        myLinkedList.display();//110 1 12 22 32 42 100
        System.out.println("=================");
        System.out.println("链表长度为:"+myLinkedList.size());//7
        System.out.println("=================");
        System.out.println("链表中是否有元素12:"+myLinkedList.contains(12));//true
        System.out.println("=================");
        int n = 4;
        Node ret = myLinkedList.findN(n);
        System.out.println("链表第"+n+"个节点是:"+ret.val);
        System.out.println("=================");
        System.out.println("421是否在链表中"+myLinkedList.contains(421));
        System.out.println("==================");
        ret = myLinkedList.findLastTwoNode();
        System.out.println(ret.val);
        System.out.println("虽然发生了异常,但是我还是想打印这句话");
        System.out.println("==================");
        myLinkedList.remove(1);
        myLinkedList.display();//110 12 22 32 42 100
        System.out.println("==================");
        Node ret1 = myLinkedList.reverseList();
        myLinkedList.display1(ret1);//100 42 32 22 12 110
        myLinkedList.clear();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值