Java单链表的基本操作

Java单链表的基本操作
1,打印单链表
2,头插法
3,尾插法
4,查找链表中是否包含关键字key
5,求单链表的长度
6,任意位置插入,第一个数据节点为0号下标
7,删除值为key的节点
8,删除所有值为key的节点

    class Node {
        public int data;
        public Node next;

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


    }

    public class MyLinkedList {
        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);
            if(this.head==null) {
                //第一次插入节点
                this.head=node;
                return;
            }
            node.next=this.head;
            head=node;
        }

        //尾插法
        public  void addLast(int data) {
            Node node=new Node(data);
            if(this.head==null) {
                this.head=node;
                return;
            }
            Node cur=this.head;
            while(cur.next!=null) {
                cur = cur.next;
            }
            cur.next=node;
        }

        //查找链表中是否包含关键字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 int size() {
            int count=0;
            Node cur=this.head;
            while(cur!=null) {
                count++;
                cur=cur.next;
            }
            return count;
        }

        //任意位置插入,第一个数据节点为0号下标
        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=searchIndex(index);
            node.next=cur.next;
            cur.next=node;
        }

        private Node searchIndex(int 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;
        }

        private Node searchPrev(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 remove(int key) {
            if(this.head==null) {
                return;
            }
            if(this.head.data==key) {
                this.head=this.head.next;
                return;
            }
            Node prev=searchPrev(key);
            if(prev==null) {
                System.out.println("没有这个节点");
                return;
            }
            Node del=prev.next;
            prev.next=del.next;
        }

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

public class TestDemo {
        public static void main(String[] args) {
            MyLinkedList myLinkedList=new MyLinkedList();
            myLinkedList.addFirst(10);
            myLinkedList.addFirst(11);
            myLinkedList.addLast(12);
            myLinkedList.addLast(13);
            myLinkedList.display();
            System.out.println(myLinkedList.contains(12));
            System.out.println(myLinkedList.size());
            myLinkedList.addIndex(0,99);
            myLinkedList.display();
            myLinkedList.remove(99);
            myLinkedList.display();
            myLinkedList.addFirst(10);
            myLinkedList.addFirst(11);
            myLinkedList.addLast(10);
            myLinkedList.addLast(13);
            myLinkedList.display();
            myLinkedList.removeAllKey(10);
            myLinkedList.display();
        }
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值