List of operations, including the head of the list, in the middle, add and delete the end

Package com.rao.linkList; 

/ ** 
 * @author Srao 
 * @className LinkList 
 * @date 2019/12/3 10:39 
 * @package com.rao.linkList 
 * @Description 
 * / 
public  class LinkList {
     / ** 
     * defined node, starting from the subscript. 1 
     * / 
    static  class the node {
         public Integer Data;
         public the node next;
         public the node (Integer Data) {
             the this .data = Data; 
        } 
    } 

    // define header node 
    public the node head; 

    / **
     * For initializing a list 
     * @param head
      * / 
    public LinkList (the Node head) {
         the this .head = head; 
    } 

    public LinkList () { 
    } 

    // The data lookup nodes 
    public the Node findNodeByData (Integer data) { 
        the Node the currentNode = head;
         the while (the currentNode =! null &&! currentNode.data.equals (Data)) { 
            the currentNode = currentNode.next; 
        } 
        return the currentNode; 
    } 

    // find the node according to index 
    publicFindNodeByIndex the Node ( int index) {
         int NUM =. 1 ; 
        the Node the currentNode = head;
         the while (the currentNode =! Null && index =! NUM) { 
            the currentNode = currentNode.next; 
            NUM ++ ; 
        } 
        return the currentNode; 
    } 

    // insert elements ( specified element inserted rearward) 
    public  void insertNode ( int index, the Node Node) {
         IF (index == 0 ) { 
            node.next =  head;
            head = node;
            return;
        }
        Node currentNode = findNodeByIndex(index);
        Node nextNode = currentNode.next;
        currentNode.next = node;
        node.next = nextNode;
    }

    //根据值删除结点
    public void deleteNodeByData(Integer data){
        Node currentNode = head;
        Node preNode = null;
        while (currentNode != null && !currentNode.data.equals(data)){
            preNode = { currentNode;
            the currentNode = currentNode.next; 
        } 
        IF (preNode == null ) { // first hit 
            IF (head.next =! null ) { 
                head = head.next; 
            } the else { 
                head = null ; 
            } 

        } the else  IF (the currentNode == null ) { // last hit 
            preNode.next = null ; 
        } the else 
            preNode.next = currentNode.next;
        }
    }

    public static void main(String[] args) {
        LinkList linkList = new LinkList(new Node(100));
        linkList.insertNode(1, new Node(3));
        linkList.insertNode(1, new Node(4));
        linkList.insertNode(1, new Node(5));//向中间插入
        linkList.insertNode(0, new Node(20));//向头插入
        linkList.insertNode (. 5, newthe Node (21 is)); // inserted into the tail 
        System.out.println (linkList.findNodeByIndex (2 ) .data); 
        System.out.println (linkList.findNodeByData ( . 5 ) .next.data) ; 
        System.out.println (linkList.findNodeByIndex ( . 6 ) .data); 
        linkList.deleteNodeByData ( 100); // delete the intermediate node 
        linkList.deleteNodeByData (20 is); // remove header node 
        linkList.deleteNodeByData (21); // remove the tail node 
        System.out.println (linkList.head.data); 
    } 
}

Be sure to add and remove points of the discussion, but also on the border situation after the written test the code, as long as the ideas straightened, write code that is not a big problem

Guess you like

Origin www.cnblogs.com/rao11/p/11975924.html