Java实现无头单向非循环链表

创建空的成员变量并用构造函数初始化

class Node {
    public int data;
    public Node next;

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

遍历链表

public class MyLinkLest {
    public Node head;//标识头节点

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

头插法

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = this.head;
        this.head = node;
    }

尾插法

   //尾插法
    public void addLast(int data) {
        //0、判断当前列表是否是第一次插入
        //1、找尾巴,cur.next == null
        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;
        }
    }

在任意位置插入,第一个数据节点为0号下表

   public boolean checkIndex(int index) {
        if (index < 0 || index > getLength()) {
            System.out.println("下标不合法");
            return false;
        }
        return true;
    }

    //在任意位置插入,第一个数据节点为0号下表
    public void addIndex(int index, int data) {
        if(!checkIndex(index)) {
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == this.getLength()) {
            addLast(data);
            return;
        }
        Node cur = searchPrev(index);
        //cur此时保存的就是index-1位置的节点的引用
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
    }

    public Node searchPrev(int index) {
        Node cur = this.head;
        int count = 0;
        while (count < index -1 ) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

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

删除第一次出现关键字为key节点

    public Node searchPrevNode (int key) {
        Node cur = this.head;
        if(cur.next != null) {
            if(cur.next.data == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key节点
    public void remove(int key) {
        if(this.head == null)
            return;
        //头节点是删除的节点
        if(this.head.data == key) {
            this.head = this.head.next;
            return;
        }
        Node cur =searchPrevNode(key);
        if(cur == null) {
            System.out.println("没有你要删除的数字");
            return;
        }
        Node del = cur.next;//删除的节点
        cur.next = del.next;
    }

删除所有值为key的节点

   public void removeAllkey(int key) {
        Node cur = new Node(key);
        cur.next = head;
        while(cur.next != null) {
            if (cur.next.data == key) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
    }

查找是否包含关键字key

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

测试代码

public class Test {
    public static void main(String[] args) {
        MyLinkLest  myLinkLest = new  MyLinkLest();
        myLinkLest.addFirst(1);
        myLinkLest.addFirst(2);
        myLinkLest.addFirst(3);
        myLinkLest.addFirst(2);
        myLinkLest.display();
        myLinkLest.addLast(1);
        myLinkLest.addLast(2);
        myLinkLest.addLast(3);
        myLinkLest.display();
        myLinkLest.addIndex(0,1);
        myLinkLest.addIndex(1,2);
        myLinkLest.addIndex(0,3);
        myLinkLest.display();
        myLinkLest.remove(1);
        myLinkLest.display();
        System.out.println(myLinkLest.contains(3));
        myLinkLest.removeAllkey(2);
        myLinkLest.display();
    }
}

完整代码

/**
 * @Author 袁媛
 * @Date 2020/7/28
 * @Time 10:04
 */
class Node {
    public int data;
    public Node next;

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

public class MyLinkLest {
    public Node head;//标识头节点

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

    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data) {
        //0、判断当前列表是否是第一次插入
        //1、找尾巴,cur.next == null
        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;
        }
    }

    public boolean checkIndex(int index) {
        if (index < 0 || index > getLength()) {
            System.out.println("下标不合法");
            return false;
        }
        return true;
    }

    //在任意位置插入,第一个数据节点为0号下表
    public void addIndex(int index, int data) {
        if(!checkIndex(index)) {
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == this.getLength()) {
            addLast(data);
            return;
        }
        Node cur = searchPrev(index);
        //cur此时保存的就是index-1位置的节点的引用
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
    }

    public Node searchPrev(int index) {
        Node cur = this.head;
        int count = 0;
        while (count < index -1 ) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

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

    public Node searchPrevNode (int key) {
        Node cur = this.head;
        if(cur.next != null) {
            if(cur.next.data == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key节点
    public void remove(int key) {
        if(this.head == null)
            return;
        //头节点是删除的节点
        if(this.head.data == key) {
            this.head = this.head.next;
            return;
        }
        Node cur =searchPrevNode(key);
        if(cur == null) {
            System.out.println("没有你要删除的数字");
            return;
        }
        Node del = cur.next;//删除的节点
        cur.next = del.next;
    }

    //删除所有值为key的节点
    public void removeAllkey(int key) {
        Node cur = new Node(key);
        cur.next = head;
        while(cur.next != null) {
            if (cur.next.data == key) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
    }

    //查找是否包含关键字key
    public boolean contains(int key) {
       Node cur = this.head;
       while( cur != null) {
           if (cur.data == key) {
               return true;
           }
          cur = cur.next;
       }
       return false;
    }
}
public class Test {
    public static void main(String[] args) {
        MyLinkLest  myLinkLest = new  MyLinkLest();
        myLinkLest.addFirst(1);
        myLinkLest.addFirst(2);
        myLinkLest.addFirst(3);
        myLinkLest.addFirst(2);
        myLinkLest.display();
        myLinkLest.addLast(1);
        myLinkLest.addLast(2);
        myLinkLest.addLast(3);
        myLinkLest.display();
        myLinkLest.addIndex(0,1);
        myLinkLest.addIndex(1,2);
        myLinkLest.addIndex(0,3);
        myLinkLest.display();
        myLinkLest.remove(1);
        myLinkLest.display();
        System.out.println(myLinkLest.contains(3));
        myLinkLest.removeAllkey(2);
        myLinkLest.display();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值