带头单向非循环链表简单应用

package com.bit.bbb.SingleLinkList;

public interface ISingleLinkList {

    void addFirst(int data);
    void addLast(int data);
    boolean addindex(int index,int data);
    boolean contains(int key);
    int remove(int key);
    void removeAllkey(int key);
    int getLength();
    void display();
    void clear();

}
public class MySingleLinkList implements ISingleLinkList {
    class Node{
        private int data;
        private  Node next;
        public Node(int data) {
            this.data=data;
            this.next=null;
        }
    }
      private  Node head;
      public MySingleLinkList(){
        this.head=null;
    }


    @Override
    public void addFirst(int data) {
     Node node=new Node(data);
     if(this.head!=null){
        node.next=this.head;
        this.head=node;
    } else
            this.head=node;
}

    @Override
    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;

        }
    }

    @Override
    public boolean addindex(int index, int data) {
        Node node=new Node(data);
        if(index<0){
            System.out.println("插入位置不合法");
            return false;
        }
        if(index==0){
            node.next=this.head;
            this.head=node;
            return true;
        }
      else {
            if (this.head == null) {
                return false;
            }
            int count=0;
            Node cur=this.head;
            Node front =null;
            while(cur!=null){
                if(count==index) {
                     front.next=node;
                     node.next=cur;
                    return true;
                }
                else {
                    front=cur;
                    cur=cur.next;
                    count++;
                }
            }
            if(index>=count){
                System.out.println("插入位置不合法");
                return false;
            }
        }
        return false;
        }


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

    @Override
    public int remove(int key) {
        if(this.head==null){
            System.out.println("单链表为空");
            return -1;
        }
        if(this.head!=null) {
            Node cur = this.head;
            Node front = this.head;
            while (cur != null) {
                if (cur.data == key) {
                    front.next = cur.next;
                    return cur.data;
                }
                    front = cur;
                    cur = cur.next;

            }
        }
        return -1;
    }

    @Override
    public void removeAllkey(int key) {

        if (this.head == null) {
            System.out.println("单链表为空");
        }
        if (this.head != null) {
            Node cur = this.head;
            Node front = this.head;
            while (cur != null) {
                if (cur.data == key) {
                    front.next = cur.next;
                }

                    front = cur;
                    cur = cur.next;

            }
        }
    }

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

    @Override
    public void display() {
        if(this.head==null){
          System.out.println("单链表为空");
        }

        if(this.head!=null){
            Node cur=this.head;
            while(cur!=null){
                System.out.print(cur.data+" ");
                cur=cur.next;
            }
            System.out.println();
        }
    }

    @Override
    public void clear() {
        if (this.head == null) {
            System.out.println("单链表为空");
        }

        if (this.head != null) {
            this.head = null;
        }
    }

}

public class Test {
    public static void main(String[] args) {
    MySingleLinkList mySingleLinkList=new MySingleLinkList();
        mySingleLinkList.addFirst(10);
        mySingleLinkList.addFirst(20);
        mySingleLinkList.addFirst(30);
        mySingleLinkList.addFirst(40);
        mySingleLinkList.addLast(50);
        mySingleLinkList.addLast(20);
        mySingleLinkList.addLast(70);
        System.out.println(mySingleLinkList.getLength());
        mySingleLinkList.display();
        System.out.println( mySingleLinkList.addindex(-1,80));
        mySingleLinkList.display();
        System.out.println(mySingleLinkList.contains(70));
        System.out.println(mySingleLinkList.remove(20));
        //mySingleLinkList.removeAllkey(20);
        mySingleLinkList.display();
        mySingleLinkList.clear();
        mySingleLinkList.display();

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值