【java】数据结构---单链表(2)

这次的单链表中加入了尾插以及从指定位置插入,以及删除链表中所有为Key的元素。

public interface IMySingleList {
    public interface ILinked {
        //头插法
        void addFirst(int data);
        //尾插法
        void addLast(int data);
        //任意位置插入,第一个数据节点为0号下标
        boolean addIndex(int index,int data);
        //查找是否包含关键字key是否在单链表当中
        boolean contains(int key);
        //删除第一次出现关键字为key的节点
        int remove(int key);
        //删除所有值为key的节点
        void removeAllKey(int key);
        //得到单链表的长度
        int getLength();
        void display();
        void clear();
    }
}


public class MySingleList implements  IMySingleList.ILinked{
    private Node head;
    private int size;
    public MySingleList(){
        this.head=null;
        this.size=0;
    }
    private class Node{
        int data;
        Node next;
        public Node(int data){
            this.data=data;
            this.next=null;
        }
    }
    public boolean isEmpty(){
        return this.head==null;
    }
    @Override
    public void addFirst(int data) {
        Node newHead=new Node(data);
        if(isEmpty()){
            head=newHead;
        }else{
            newHead.next=head;
            head=newHead;
        }
        size++;
    }

    @Override
    public void addLast(int data) {
        Node node=new Node(data);
        Node cur=head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
        size++;
    }

    @Override
    public boolean addIndex(int index, int data) {
        if(index<0||index>size) {
            return false;
        }else {
            Node node=new Node(data);
            Node dumyHead=new Node(-1);
            dumyHead.next=head;
            Node cur=head;
            Node prev=dumyHead;
            while (index!=0){
                cur=cur.next;
                prev=prev.next;
                index--;
            }
            prev.next=node;
            node.next=cur;
        }
        this.size++;
        return true;
    }

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

    @Override
    public int remove(int key) {
        if (isEmpty()){
            throw new UnsupportedOperationException("链表为空");
        }
        Node cur=head;
        Node prev=new Node(-1);
        prev.next=head;
        while (cur.data!=key){
            if(cur.next==null){
                return 0;
            }else {
                prev=prev.next;
                cur=cur.next;
            }
        }
        if(cur==head){
            head=head.next;
            this.size--;
        }else {
            prev.next=cur.next;
            this.size--;
        }
        return 0;
    }

    @Override
    public void removeAllKey(int key) {
        if (isEmpty()){
            throw new  UnsupportedOperationException("链表为空");
        }
        Node cur=head;
        Node prev=new Node(-1);
        prev.next=head;
        int count=0;
        while (cur!=null){
            if (cur.data==key){
                remove(key);
                cur=cur.next;
            }else {
                cur=cur.next;
                prev=prev.next;
            }
        }
    }

    @Override
    public int getLength() {
        return this.size;
    }

    @Override
    public void display() {
        Node cur = head;
        if (isEmpty()) {
            System.out.println("[]");
        } else if (cur.next == null) {
            System.out.println("[" + cur.data + "]");
        } else {
            for (cur = head; cur != null; cur = cur.next) {
                if (cur == head) {
                    System.out.print("[" + cur.data + "->");
                }else if(cur.next==null){
                    System.out.print(cur.data+"]");
                }else {
                    System.out.print(cur.data+"->");
                }
            }
            System.out.println();
        }
    }
    @Override
    public void clear() {
        while (head!=null){
            head=head.next;
            this.size--;
        }
    }
}

public class TestMySingleList {
    public static void main(String[] args) {
        MySingleList mySingleList=new MySingleList();
        mySingleList.addFirst(1);
        mySingleList.addFirst(3);
        mySingleList.addLast(2);
        mySingleList.addLast(4);
        mySingleList.addIndex(2,3);
        mySingleList.display();
        mySingleList.removeAllKey(3);
        mySingleList.display();
        mySingleList.remove(4);
        mySingleList.display();
        System.out.println(mySingleList.contains(1));
        System.out.println(mySingleList.contains(6));
        mySingleList.clear();
        System.out.println(mySingleList.getLength());
    }
}

运行结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值