数据结构单链表的模拟实现


import java.util.Stack;

public class SingleLinkedList {
     static class Node{
        public int value;
        Node next;
        public Node(int value) {
            this.value = value;
        }

    }
    public Node head;
    public int size(){
        int count=0;
        Node current =head;
        while(current!=null){
            current=current.next;
            count++;
        }
        return count;
    }
    public void addFirst(int data) {
            Node node = new Node(data);
            node.next = head;
            head = node;

    }
    public void addLast(int data){
        Node node=new Node(data);
        if(head==null){
            addFirst(data);
        }
        else{
            Node current=head;
            while(current.next!=null){
                current=current.next;
            }
            current.next=node;
        }
    }
    public void addIndex(int index,int data){
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        check(index);
        Node previousNode=FindPreviousNode(index);
        Node node=new Node(data);
        node.next=previousNode.next;
        previousNode.next=node;

    }
    private void check(int index){
        if(index<0||index>size())
            throw new ArrayIndexOutOfBoundsException("index位置不合法");
    }
    private Node FindPreviousNode(int index){
        Node current =head;
        for(int i=0;i<index-1;i++){
            current=current.next;
        }
        return current;
    }
    public boolean contains(int key){
        Node current =head;
        while(current!=null){
            if(current.value==key) {
                return true;
            }
            current=current.next;
        }
        return false;
    }
public void remove(int key){
        if(head.value==key){
            head=head.next;
            return;//r如果删除的是首元素
        }
        //如果删除的是中间元素
        Node current=head;
        while(current.next!=null){
            if(current.next.value==key){
                current.next=current.next.next;
                break;//不退出会报空指针异常
            }
            current=current.next;
        }
}
public void removeAllKey(int key){
        if(head==null){
            return;
        }
        Node previousNode=head;
        Node current=head.next;
        while(current!=null){
            if(current.value==key){
                previousNode.next=current.next;
            }else{
                previousNode=current;
            }
            current=current.next;
        }
        if(head.value==key){
            head=head.next;
        }
}

public void clear(){
        Node current=head;
        while(current!=null){
            Node nextNode=current.next;
            current.next=null;
            current=nextNode;
        }
      head=null;

}
   
   
    public  void display(){
        Node current=head;
        while(current!=null){
            System.out.print(current.value+" ");
            current=current.next;
        }
        System.out.println();
    }
    public static void display(Node head){
        Node current=head;
        while(current!=null){
            System.out.print(current.value+" ");
            current=current.next;
        }
        System.out.println();
    }
    public void printList(){
        //倒序遍历递归方法
        if(head==null){
            return ;
        }
        Node current=head;
        if(current.next!=null){
            printList(current.next);
        }
        System.out.println(current.value);
    }
    public void printList (Node head) {
        //倒序遍历将链表压入栈中依次弹出
        if (head == null) {
            System.out.println("");
            return;
        }
        Node current=head;
        Stack<Node> stack=new Stack<>();
        while(current!=null){
            stack.push(current);
            current=current.next;
        }
          while(!stack.isEmpty()){
              System.out.print(stack.pop().value+" ");
          }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhpan666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
>