Using Java to realize two-way cyclic link list deletion, modification and check

Source: Internet
Author: User
Java, bidirectional cyclic linked list

The annotation is clearer, the basic function is realized based on the comparison

* * Content: Use Java to realize bidirectional cyclic link List of additions and deletions to check * Time: 2017.3.3 * * * Package com.mylist;

Import Java.util.Scanner; public class MyList {public node head;//header node, data field storage chain table length//constructor, initializing linked list public MyList () {this.head = nul
    Head = new Node (0, NULL, NULL);

        //Create a linked list public Node createmylist () {head = new node (0, NULL, NULL);
        SYSTEM.OUT.PRINTLN ("Please output the value of the list sequentially:");
        int i = 0;
        Scanner input = new Scanner (;
        i = Input.nextint ();
        Node P1 = head;              while (I!= 0) { + +;                 Linked list length plus 1 node P2 = new Node (i, P1, head);//Create a new node, the data field is I, the precursor P1, followed by head formation cycle chain table Head.prev = p2;                   The precursor of head node is the last node = p2;                        The successor of the P1 is p2 p1 = p2;
        Finally P1 replaces the position of P2 and continues to create nodes i = Input.nextint ();
    return head; //Get the node of position I (count from 1, different from array) public node getnode (int i,node node{Node pnode = null;
        Feedback if the position of I exceeds the bounds of the list if (I <= 0 | | i > {System.out.println ("Get Failed");
            else {pnode = node;
                while (i > 0) {pnode =;
            SYSTEM.OUT.PRINTLN ("Node data domain is:";
    } return Pnode;   //Add: Inserts a node at the I location of the linked table node whose data field value is x public boolean insertmylist (Node node,int i,int x) {Boolean b = false;
        Returns a Boolean type that is used to interpret whether the operation was successful Node Pnode = GetNode (i, node); if (Pnode = = null) {System.out.println ("Insert failed.") Position is out of bounds.
            }else {Node PNode2 = new node (x, Pnode.prev, pnode);//inserted node = PNode2;
            Pnode.prev = PNode2;  + +;
        List length plus 1 B = true;
    return b; //Delete: Delete node public boolean deletemylist on location I (node Node,int i) {BooLean B = false;
        Node Pnode = getnode (i, node); if (Pnode = = null) {System.out.println ("delete failed.") Position is out of bounds.
            else { =;
   = Pnode.prev;
        Pnode = null;//to empty the deleted object, waiting for the virtual machine to reclaim memory;//list length minus 1 B = true;
    return b;

        ///change: The data field that modifies the node at position i is x public boolean changemylist (node Node,int i,int x) {Boolean b = false;
        Node Pnode = getnode (i, node); if (Pnode = = null) {SYSTEM.OUT.PRINTLN ("failed to modify.") Position is out of bounds.
            else { = x;
        b= true;
    return b;   Check: Data domain public int querymylist (node node,int i) {int x =-999 for node of query location I;
        Set the default value, should not be set to 0, it is possible that the list has elements of the value is 0 Node pnode = getnode (i, Node); if (Pnode = = null) {System.out.println ("Query failed.") Position is out of bounds.
        else {x =; } return X ///Output list public void Printmylist (node node) {if (node = null | | = 0) {System.out. println ("The linked list is empty.)
        Node pnode =;
        SYSTEM.OUT.PRINTLN ("Linked list length is";
        SYSTEM.OUT.PRINTLN ("Output list is:");
            while (pnode!= node) {System.out.println (;
        Pnode =; } public static void Main (string[] args) {//TODO auto-generated method stub MyList MyList = N

        EW MyList ();
        Create a linked list Mylist.head = Mylist.createmylist ();

        Output chain table mylist.printmylist (Mylist.head); Add/*if (Mylist.insertmylist (mylist.head, 1, 0)) {System.out.println ("Insert succeeded.")
        Mylist.printmylist (Mylist.head); }*///Delete/*if (Mylist.deletemylist (Mylist.head, 4)) {System.out.println ("delete succeeded.")
        Mylist.printmylist (Mylist.head);

        }*/Change/*if (Mylist.changemylist (mylist.head, 5, 0)) {System.out.println ("modified successfully");
        Mylist.printmylist (Mylist.head);
            }*///Check if (Mylist.querymylist (Mylist.head, 4)!= -999) {System.out.println ("successful query"); SYSTEM.OUT.PRINTLN (The data field on the "I position is" +mylist.querymylist (Mylist.head, 4)),//i must be consistent with the preceding//mylist.printmylist (myl
        Ist.head);    }}//Node class node{protected node prev;    Pioneer protected Node Next;     subsequent protected int data;
        data Field//constructor public Node (int data,node prev,node next) { = data;
        This.prev = prev; = Next; }

Finally, there are two small questions about the list.
1. Find the middle node of single linked list
2, find a single linked list of the penultimate m node

The list above is not cyclic ... It's boring.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.