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
L
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 (system.in);
i = Input.nextint ();
Node P1 = head; while (I!= 0) {this.head.data + +; 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 p1.next = 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 > Node.data) {System.out.println ("Get Failed");
else {pnode = node;
while (i > 0) {pnode = Pnode.next;
I--;
SYSTEM.OUT.PRINTLN ("Node data domain is:" +pnode.data);
} 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 PNode.prev.next = PNode2;
Pnode.prev = PNode2; Node.data + +;
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.next = Pnode.next;
PNode.next.prev = Pnode.prev;
Pnode = null;//to empty the deleted object, waiting for the virtual machine to reclaim memory Node.data--;//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 {pnode.data = 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 = Pnode.data; } return X ///Output list public void Printmylist (node node) {if (node = null | | node.data = 0) {System.out. println ("The linked list is empty.)
");
return;
Node pnode = Node.next;
SYSTEM.OUT.PRINTLN ("Linked list length is" +node.data);
SYSTEM.OUT.PRINTLN ("Output list is:");
while (pnode!= node) {System.out.println (pnode.data);
Pnode = Pnode.next; } 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) {this.data = data;
This.prev = prev;
This.next = 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.