Java实现单链表

在数据结构中,链表的结构,就是一个个节点连接在一起,形成一个完整的链条,每个节点包含2部分,数据域,和一个指向下一个节点引用的指针next
下面是代码:

package Linked.List;
/**
 * ClassName: SingleLinkedList
 * Description: 1.无头单向非循环链表实现
 * @since JDK 1.8
 */
class Node {
    //节点类型
    public int data;//数据域
    public Node next;//下一个节点引用

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}
public class SingleLinkedList {

    public Node head;   //保存单链表的头节点的引用
    
    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        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;
        }
    }

    //任意位置插入一个数据节点为data号的下标
    public void addIndex(int index, int data) {
        Node node = new Node(data);
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == this.size()) {
            addLast(data);
            return;
        }
        //先找到 index位置的前一个节点的地址
        Node cur = searchIndext(index);
        //进行插入
        node.next = cur.next;
        cur.next = node;
    }
    private Node searchIndext(int index) {
        //1.检查index的合法性
        if (index < 0 || index > this.size()) {
            throw new RuntimeException("index位置不合法");
        }
        Node cur = this.head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = this.head;
        while (cur != null) {
            if (cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        //删除的是不是头节点
        if (this.head.data == key) {
            this.head = this.head.next;
            return;
        }
        //调用找到删除节点的前驱
        Node prev = searckkey(key);
        if (prev == null) {
            System.out.println("没有节点");
            return;
        }
        Node del = prev.next;
        //开始删除
        prev.next = del.next;
    }
    private Node searckkey(int key) {
        Node prev = this.head;
        while (prev.next != null) {
            if (prev.next.data == key) {
                //找到前驱
                return prev;
            } else {
                prev = prev.next;
            }
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        //prev前驱节点
        //cur代表要删除的节点
        Node prev = this.head;
        Node cur = prev.next;
        while (cur != null) {
            if (cur.data == key) {
                //删除元素
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.data == key) {
            this.head = this.head.next;
        }
    }

    //得到单链表的长度
    public int size() {
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //打印单链表
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    //释放内存
    public void clear() {
        this.head = null;
    }

然后是部分函数的测试代码:

package Linked.List;

/**
 * ClassName: TestSingList
 * Description:测试单链表
 * @since JDK 1.8
 */

public class TestSingList {
    public static void main(String[] args) {
        SingleLinkedList myLinkList = new SingleLinkedList();
        System.out.println("===================尾插法=====================");
          myLinkList.addLast(14);
          myLinkList.addLast(15);
          myLinkList.addLast(16);
          myLinkList.addLast(17);
          myLinkList.addLast(18);
          myLinkList.display();
        System.out.println("===================头插法=====================");
          myLinkList.addFirst(18);
          myLinkList.display();
        System.out.println("===================在1位置插入================");
          myLinkList.addIndex(1,2);
          myLinkList.display();
        System.out.println("===================删除删除值为2的节点=========");
          myLinkList.remove(2);
          myLinkList.display();
        System.out.println("===================删除删除所有值为18的节点====");
         myLinkList.removeAllKey(18);
         myLinkList.display();
    }
}

测试结果
在这里插入图片描述
此博客内容为个人学习所总结,如有问题存在或描述不当,望及时指出,谢谢

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值