java achieve singly linked list

class the Node {
     public  int Val;
     public the Node Next; 
    
    public the Node ( int Val) {
         the this .val = Val; 
    } 
    
} 

class LinkList {
     Private the Node head;
     Private the Node tail; 
    
    // list object node increasing 
    public  void the addNode (the Node Node) {
         IF ( the this .head == null ) { // list initialization, the first node of 
            the this .head = node;
             the this .tail = node;
             the this= .tail.next null ; 
        } 
        the else  // list last node mount 
        {
             the this .tail.next = Node;
             the this .tail = Node; 
        } 

    } 
    
    // query nodes 
    public the Node the getNode ( int Val) 
    { 
        the Node TEMP = the this . head;
         the while (! TEMP = null ) 
        { 
            IF (temp.val == Val) 
            { 
                return TEMP; 
            } 
            TEMP =temp.next; 
        } 
        return  null ; 
        
        
    } 
    
    // modify the value of the node 
    public  void updateNode ( int Val, int value) { 
        the Node TEMP = the this .head;
         the while (TEMP =! null ) 
        { 
            IF (temp.val == Val) 
            { 
                temp.val = value; 
            } 
            TEMP = temp.next; 
        } 
    } 
    
    // remove nodes 
    public the node deleteNode ( intVal) { 
        the Node TEMP = the this .head;
         IF ( the this .head.val == Val) 
        { 
            the this .head = the this .head.next;
             return TEMP; 
        } 

        
        the while ! (temp.next = null ) 
        {     
            
            IF (TEMP. == Val next.val) // value of the next node of the current node is directly determined;
                                                       // at a node determines it must not be null, that is, only to the penultimate node is determined to 
            {     
                the node delNode = TEMP. the Next; 
                temp.nexttemp.next.next =;   // next node in the current node is directly connected to delete a node in order to achieve the next current node 
                return delNode; 
            } 
            
            TEMP = temp.next; 
            
            
        } 
        
        return  null ; 
    } 
    
    // print all nodes value 
    public  void getAllNodes () { 
        the Node TEMP = the this .head;
         the while (TEMP =! null ) 
        { 
            of System.out.print (temp.val + "" ); 
            TEMP = temp.next; 
        } 
        System.out.println ( ); 
    } 
}

public class TestLink
{
    public static void main(String[] args){
        LinkList ll=new LinkList();
        ll.addNode(new Node(1));
        ll.addNode(new Node(2));
        ll.addNode(new Node(3));
        ll.addNode(new Node(4));
        if (ll.getNode(3)!=null)
        {
            System.out.println(ll.getNode(3).val);
        }
        if (ll.getNode(5)!=null)
        {
            System.out.println(ll.getNode(5).val);
        }
        
        ll.updateNode(1,100);
        ll.updateNode(4,400);
        ll.updateNode(2,200);
        ll.updateNode(5,500);
        
        ll.addNode(new Node(5));
        ll.addNode(new Node(6));

        
        System.out.println("************");
        ll.deleteNode(6);
        ll.getAllNodes();
        ll.deleteNode(3);
        ll.deleteNode(1);
        ll.deleteNode(100);
        System.out.println("************");
        ll.getAllNodes();

    }
}

Guess you like

Origin www.cnblogs.com/xiaoxiao075/p/12055502.html