3, circular list (java implementation)

1, node class

public class Node<T> {
    public T data;
    public Node next;
}

2, the implementation class

public class CircularLink<T> {

    private static Node head = null;

    /**
     * Initialization
     */
    public void initCircularLink() {
        head = new Node();
        head.data = null;
        head.next = head;
    }

    /**
     * Insert node
     *
     * @Param Element: Node element value
      * / 
    public  void insertCircularLink (T Element) {
        Node the Node = new new the Node (); // initialize node 
        node.data = Element;
         IF (head.next == head) {
            head.next = node;
            node.next = head;
        } else {
            Node temp = head;
            while (temp.next != head) {
                temp = temp.next;
            }
            temp.next = node;
            node.next = head;

        }
    }

    /**
     * Delete value
     *
     * @Param Element: the value to be deleted
     * @Return : Delete is true, false otherwise
      * / 
    public  boolean deleteCircularLink (T Element) {

        Node temp = head;
        if (temp.next == head) {
            System.out.println ( "the list is empty, no value can be deleted" );
             return  false ;
        }
        while (temp.next != head) {
            if (temp.next.data == element) {
                temp.next = temp.next.next;
                return true;
            } else {
                temp = temp.next;
            }
        }
        return false;
    }

    /**
     * Subscript deleted
     *
     * @Param i: To delete the value of the subscript
     * @Return : Delete is true, false otherwise
      * / 
    public  boolean deleteIndexCircularLink ( int i) {


        Node temp = head;
        int index = -1;
        if (temp.next == head) {
            System.out.println ( "the list is empty, no value can be deleted" );
             return  false ;
        }
        if (i < 0 || i >= sizeCircularLink()) {
            System.out.println ( "subscript out of range" );
             return  false ;
        }
        while (temp.next != head) {
            index++;
            if (index == i) {
                temp.next = temp.next.next;
            }
            temp = temp.next;
        }
        return false;

    }

    /**
     * Find index setpoint
     *
     * @Param Element: looking element
      * / 
    public  void findCircularLink (T Element) {
        Node temp = head;
        int index = -1;
        if (temp.next == head) {
            System.out.println ( "the list is empty, unable to find" );

        }
        while (temp.next != head) {
            temp = temp.next;
            index++;
            if (temp.data == element) {
                temp = temp.next;
                System.out.println ( "subscript is:" + index);
                 return ;
            }
        }
        System.out.println ( "value you're looking for is not here" );
    }

    /**
     * Index evaluation
     * @param index
     */
    public void findDataCircularLink(int index) {
        TEMP the Node = head;
         int size = 0;            // increase index with the 
        IF (temp.next == head) {
            System.out.println ( "the list is empty, no value can be deleted" );
             return ;
        }
        if (index >= sizeCircularLink() || index < 0){
            System.out.println ( "Your subscript out of range" );
             return ;
        }

        while (temp.next !=head){
            temp = temp.next;

            if (size == index){
                System.out.println ( "you're looking for is:" + temp.data);
                 return ;
            }
            size++;
        }
        System.out.println ( "you have no value" );
    }

    /**
     * Print
     */
    public void printCircularLink() {
        Node temp = head;
        Of System.out.print ( "Print the circular list:" );
         the while (! = Temp.next head) {
            temp = temp.next;
            System.out.print(temp.data + " ");

        }
        System.out.println();
    }


    /**
     * Seek length
     *
     * @Return : returns the length
      * / 
    public  static  int sizeCircularLink () {
        Node temp = head;
        int size = 0;
        while (temp.next != head) {
            temp = temp.next;
            size++;
        }
        return size;
    }

    public static void main(String[] args) {
        CircularLink<Integer> circularLink = new CircularLink();
        circularLink.initCircularLink();
        circularLink.insertCircularLink(1);
        circularLink.insertCircularLink(2);
        circularLink.insertCircularLink(3);
        circularLink.insertCircularLink(4);
        circularLink.insertCircularLink(5);

        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println ( "value Delete value" );
        circularLink.deleteCircularLink(1);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println ( "Delete index value" );
        circularLink.deleteIndexCircularLink(1);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println ( "Find the value of the subscript" );
        circularLink.findCircularLink(5);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

        System.out.println ( "index lookup value" );
        circularLink.findDataCircularLink(3);
        circularLink.printCircularLink();
        System.out.println("长度: " + circularLink.sizeCircularLink());
        System.out.println();

    }
}

3, the test results

Printing circular list: 12345  
length: 5

Delete the value value
Printing circular list: 2345  
Length: 4

Delete index value
Printing circular list: 245  
Length: 3

Find the value of the subscript
Subscript: 2 
Print circular list: 245  
Length: 3

Find the value of the subscript
Your index out of bounds
Printing circular list: 245  
Length: 3

 

Guess you like

Origin www.cnblogs.com/karrya/p/11030211.html