其他分享
首页 > 其他分享> > 2021-10-24

2021-10-24

作者:互联网

package data;

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

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

public class singlelist {
    private int size;
    private Node head;
    public void addFirst(int data){
        if (size==0){
            Node node=new Node(data);
            head=node;
            size++;
        }else
        {
            Node node=new Node(data);
            node.next=head;
            head=node;
            size++;
        }
    }
 public void addIndex(int index,int data){
        if (index<0||index>size){
            System.out.println("wrong");
            return;
        }
        if (index==0){
            addFirst(data);
            return;
        }
        Node node=new Node(data);
        Node prev=head;
     for (int i = 0; i <index-1 ; i++) {
         prev=prev.next;

     }
     node.next=prev.next;
     prev.next=node;
     size++;
 }
 public void addLast(int data){
        addIndex(size,data);
 }
 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
 }

    @Override
    public String toString()
    {
        String ret="";
        Node node=head;
        while (node!=null){
            ret+=node.data+"->";
            node=node.next;
        }
        ret+="NULL";
        return ret;
    }
}

在这里插入图片描述index=?


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

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

public class singlelist {
    private int size;
    private Node head;
    public void addFirst(int data){
        if (size==0){
            Node node=new Node(data);
            head=node;
            size++;
        }else
        {
            Node node=new Node(data);
            node.next=head;
            head=node;
            size++;
        }
    }
 public void addIndex(int index,int data){
        if (index<0||index>size){
            System.out.println("wrong");
            return;
        }
        if (index==0){
            addFirst(data);
            return;
        }
        Node node=new Node(data);
        Node prev=head;
     for (int i = 0; i <index-1 ; i++) {
         prev=prev.next;

     }
     node.next=prev.next;
     prev.next=node;
     size++;
 }
 public void addLast(int data){
        addIndex(size,data);
 }
public int get(int index)
{
    if (rangeCheck(index)){
    Node node=head;
    for (int i = 0; i < index; i++) {
        node=node.next;
    }
    int data=node.data;
    return data;
    }
    else{
    System.out.println("wrong");
    return-1;}
}
    public  int  set(int index,int data)
{   if (rangeCheck(index)){
    Node node=head;
    for (int i = 0; i <index ; i++) {
        node=node.next;
    }
    int oldData= node.data;
    node.data=data;
    return oldData;
}else{
    System.out.println("wrong");
    return -1;
}}
    private boolean rangeCheck(int index){
        if (index<0||index>size){
            return false;
        }
        return true;
    }
    public boolean contains(int data){
        Node node=head;
        while(node!=null){
            if (node.data==data){
                System.out.println("find");
                return true;
            }node=node.next;
        }
        System.out.println("false");
        return false;
    }

 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
     System.out.println(singlelist.get(2));
 }

    @Override
    public String toString()
    {
        String ret="";
        Node node=head;
        while (node!=null){
            ret+=node.data+"->";
            node=node.next;
        }
        ret+="NULL";
        return ret;
    }
}

在这里插入图片描述while(data=?)

 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
     System.out.println(singlelist.get(2));
     System.out.println(singlelist.contains(33));
 }

在这里插入图片描述
change

 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
     System.out.println(singlelist.get(2));
     System.out.println(singlelist.contains(33));
     System.out.println(singlelist.set(2,3333));
     System.out.println(singlelist);
 }

在这里插入图片描述removeFirst

 public void removeFirst(){
        /*if (head == null||head.next==null) {
            return;
        }*/
        Node node=head;
        head=head.next;
        node.next=null;
        size--;
    }

 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
    /* System.out.println(singlelist.get(2));
     System.out.println(singlelist.contains(33));
     System.out.println(singlelist.set(2,3333));*/
     singlelist.removeFirst();
     System.out.println(singlelist);
 }

在这里插入图片描述
delete

public void removeIndex(int index)
{
    if (rangeCheck(index)){
        if (index==0){
   removeFirst(); }
else{
        Node prev=head;
        for (int i = 0; i <index-1 ; i++) {
            prev=prev.next;
        }
        Node node=prev.next;
        prev.next=node.next;
        node.next=null;
    size--;} }else {
    System.out.println("wr");
}}
 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
    /* System.out.println(singlelist.get(2));
     System.out.println(singlelist.contains(33));
     System.out.println(singlelist.set(2,3333));*/
     singlelist.removeFirst();
     System.out.println(singlelist);
        singlelist.removeIndex(1);
     System.out.println(singlelist);
 }

在这里插入图片描述delete

    public void removeValueOnce(int data)
    {
        if (head.data==data){
            removeFirst();
        }else {
            Node prev=head;
            while (prev.next!=null){
                if (prev.next.data==data){
                    Node node=prev.next;
                    prev.next=node.next;
                    node.next=null;
                    size--;
                    break;
                }else {
                    prev=prev.next;
                }
            }
        }
    }
 public static void main(String[] args){
        singlelist singlelist=new singlelist();
singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
    /* System.out.println(singlelist.get(2));
     System.out.println(singlelist.contains(33));
     System.out.println(singlelist.set(2,3333));*/
     singlelist.removeFirst();
     System.out.println(singlelist);
        singlelist.removeIndex(1);
     System.out.println(singlelist);
     singlelist.removeValueOnce(4);
     System.out.println(singlelist);
 }

    @Override
    public String toString()
    {
        String ret="";
        Node node=head;
        while (node!=null){
            ret+=node.data+"->";
            node=node.next;
        }
        ret+="NULL";
        return ret;
    }
}

`


![在这里插入图片描述](https://www.icode9.com/i/ll/?i=b738b38656f84936a005739336f1be0f.png?,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY3poYzExNDAwNzU2NjM=,size_20,color_FFFFFF,t_70,g_se,x_16)![在这里插入图片描述](https://www.icode9.com/i/ll/?i=b275cb32f99b4308a3bb22b29de85df0.png?,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY3poYzExNDAwNzU2NjM=,size_20,color_FFFFFF,t_70,g_se,x_16)
delete All

  public void removeAllValue(int data)
    {
        while (head!=null&&head.data==data)
        {
            Node node=head;
          head=head.next;
          node.next=null;
          size--;
        }
        if (head==null)
        {return;
    }else {Node prev=head;
        while(prev.next!=null){
            if (prev.next.data==data) {
                Node node=prev.next;
                prev.next=node.next;
                node.next=null;
                size--;
            }else{prev=prev.next;
        }}}}
 public static void main(String[] args){
        singlelist singlelist=new singlelist();
/*singlelist.addLast(1);
singlelist.addLast(4);
singlelist.addLast(3);
singlelist.addFirst(8);
singlelist.addIndex(2,33);
     System.out.println(singlelist);
    *//* System.out.println(singlelist.get(2));
     System.out.println(singlelist.contains(33));
     System.out.println(singlelist.set(2,3333));*//*
     singlelist.removeFirst();
     System.out.println(singlelist);
        singlelist.removeIndex(1);
     System.out.println(singlelist);
     singlelist.removeValueOnce(4);
     System.out.println(singlelist);*/
     singlelist.addLast(4);
     singlelist.addLast(3);
     singlelist.addLast(3);
     singlelist.addLast(3);
     singlelist.addLast(3);
     System.out.println(singlelist);
     singlelist.removeAllValue(3);
     System.out.println(singlelist);
 }

    @Override
    public String toString()
    {
        String ret="";
        Node node=head;
        while (node!=null){
            ret+=node.data+"->";
            node=node.next;
        }
        ret+="NULL";
        return ret;
    }
}

![在这里插入图片描述](https://www.icode9.com/i/ll/?i=9a5695969242426f8fbd9307f697e16c.png?,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAY3poYzExNDAwNzU2NjM=,size_20,color_FFFFFF,t_70,g_se,x_16)







标签:24,10,node,System,Node,2021,singlelist,println,data
来源: https://blog.csdn.net/czhc1140075663/article/details/120936324