单向链表的实现

13 篇文章 0 订阅
10 篇文章 0 订阅

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

1.创建一个链表并打印:
class Node {
    public int val;
    public Node next;

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

    public Node head;//普通引用,目的是让head一直指向当前列表的头

    public void createLinked() {
        this.head = new Node(12);
        Node node2 = new Node(22);
        Node node3 = new Node(32);
        Node node4 = new Node(42);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
    }

    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.createLinked();
        myLinkedList.display();

    }

结果:
在这里插入图片描述

2.实现一个单链表

1.头插法
2.尾插法
3.任意位置插入,第一个数据节点为0号下标
4.查找是否包含关键字key是否在单链表当中
5.删除第一次出现关键字为key的节点
6.删除所有值为key的节点
7.得到单链表的长度
8.打印一个单链表
代码示例:

class Node {
    public int val;
    public Node next;

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

    public Node head;//普通引用,目的是让head一直指向当前列表的头

    public void createLinked() {
        this.head = new Node(12);
        Node node2 = new Node(22);
        Node node3 = new Node(32);
        Node node4 = new Node(42);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
    }
//7.打印一个链表
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print (cur.val +" ");
            cur = cur.next;
        }
        System.out.println();
    }
  
    //8.得到单链表的长度
    public int size(){
        Node cur = this.head;
        int count =0;
        while (cur != null ) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    //4.查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur= this.head;
        while (cur!= null){
            if (cur.val == key ){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //1.头插法
    public void addFirst(int data) {
        Node node= new Node(data);//Node一个新的节点;
        if (this.head ==null){
           this.head= node;//链表为空,链表的头就等于要插入的节点;
        }else {
            node.next = this.head;//①(先绑定后面的)新节点的next=原来链表的head;
            this.head = node;//②链表的头等于要插入的节点;
        }
    }
    //2.尾插法
    public void addLast(int data){
        Node node= new Node(data);//Node一个新的节点;
        if (this.head == null){
            this.head =node;//节点为空,链表的头就是要插入的节点;
        }else{
            Node cur = this.head;
              while (cur.next!=null){
                  cur=cur.next;//遍历找到最后一个节点;
              }
              cur.next=node;//最后一个节点的next等于要插入的节点;
        }
    }
    public Node moveYiIndex (int index){//找到要插入节点的前一个
        Node cur = this.head;
        int count =0;
        while (count != index-1){
            cur= cur.next;
            count++;
        }
        return cur;
    }
    //3.任意位置插入,第一个数据节点为0号下标
   public void addIndex(int index,int data) {
       if (index < 0 || index > size()) {//不合法现象;
           System.out.println("index不合理");
           return;
       }
       if (index == 0) {//头插法
           addFirst(data);
           return;
       }
       if (index == size()){ //尾插法
           addLast(data);
           return;
       }
       Node cur =moveYiIndex(index);//调用moveYiIndex函数,找到index的前一个节点cur;
       Node node = new Node(data);//定义一个新的节点;
       node.next= cur.next;//插入节点的next等于原始cur的next;
       cur.next = node;//cur的next变成了node;
   }
    public Node searchCur(int key){//找到要删除节点的前驱
        Node cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){//找出key的节点的前一个
                return cur;
            }
            cur = cur.next;

        }
        return null;
    }
 //5.删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
            if (this.head.val == key){
                this.head = this.head.next;
                return;
            }
        Node cur = searchCur(key);//调用searchCur,找出删除节点的前驱cur
        if (cur == null){
            System.out.println("未找到要删除的节点");
        }else{
            Node del = cur.next;//定义要删除的节点为key;
            cur.next =del.next;//让cur的next等于删除节点的next;
        }
    }
 //6.删除所有值为key的节点
     public void removeAllKey(int key){
        if (this.head == null){
            System.out.println("链表为空");
            return;
        }
        Node prev = this.head;//定义一个prev指向链表的头;
        Node cur = prev.next;//定义一个cur指向prev的next;
        while (cur != null){//遍历完条件;
            if (cur.val == key){//找到要删除的节点;
                prev.next = cur.next;//把该节点删掉;
            }else {
                prev = cur;//未找到
            }
            cur = cur.next;//cur继续往后走;
        }
        if (this.head.val == key){//头节点就是要删除的节点
            this.head = this.head.next;//删除头,链表的头直接是head的next;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值