JAVA linked list operation: cyclic chain list
Main analysis Examples:
First, single linked list cycle list
Double linked list Cycle list
The single linked table node and the Double linked table node class and interface icommoperate<t> are consistent with the previous article. Reference: Java linked list operation: Single linked list and double linked list http://www.jb51.net/article/95113.htm
First, single linked list cycle list
Package linklisttest;
Import Java.util.HashMap;
Import Java.util.Map; public class Singlecyclelinklist implements icommoperate<snode> {private Snode Header = new Snode ("Head");
The needle, after the declaration does not change the private int size = 0;
public int GetSize () {return this.size; * * * * List inserts, each to the end of the insertion, determine whether the end of the standard is next point head * */@Override public boolean Insertnode (Snode node) {Boolean
Flag = false; Initlinklist ();
Initializes the linked list if (this.size==0) {//Empty link Table this.head.setNextNode (node);
Node.setnextnode (This.head);
}else{Snode current = This.head;
while (Current.getnextnode ()!=this.head) {//Find end node current = Current.getnextnode ();
} current.setnextnode (node); Node.setnextnode (This.head);
Follow bad linked list, tail node point head} this.size++;
Flag = true;
return flag; * * * Inserts a list at the specified position pos, starting at 1, while POS is larger than size and is inserted at the end of the list * */@Override public boolean insertposnode (int pos, snode node) {Boolean Flag = true;
Snode current = This.head.getNextNode ();
Initlinklist ()///init linked list if (this.size==0) {//linked list is empty this.head.setNextNode (node);
Node.setnextnode (This.head);//Follow the bad linked list, the tail node points to head this.size++;
}else if (this.size<pos) {//POS position is greater than the chain table length, insert end insertnode (node);
}else if (pos>0 && pos<=this.size) {//Linked list node//1, locate node to insert POS position and Front node, node will insert between two nodes int find = 0; Snode Prenode = This.head; Front node Snode currentnode = current; The current node while (find<pos-1 && currentnode!=this.head) {prenode = present; The front node moves currentnode = Currentnode.getnextnode ();
The current node moves find++;
if (find<pos-1 && currentnode!=this.head) {//Before the search node is closed, the before-move node current = Current.getnextnode ();
}//System.out.println (Prenode);
System.out.println (CurrentNode); 2, insert the node Prenode.setnextnode (noDE);
Node.setnextnode (CurrentNode);
this.size++;
}else {System.out.println ("Location information error");
Flag = false;
return flag;
private void Initlinklist () {if (size==0) {this.head.setNextNode (this.head); }/* Specifies the node POS for the linked list and deletes the corresponding node.
Way: Find to delete the node before and after the node to delete, subscript starting from 1 * */@Override public boolean deletenode (int pos) {Boolean flag = false;
Snode current = This.head.getNextNode ();
if (pos<=0 | | pos>this.size | | current==this.head) {SYSTEM.OUT.PRINTLN ("Location information error or no information on linked list");
}else{//1, find to delete node before and after the node int found = 0; Snode Prenode = This.head; Front node Snode nextnode = Current.getnextnode (); Post-node while (find<pos-1 && nextnode!=this.head) {prenode = current; The front node moves nextnode = Nextnode.getnextnode ();
After the node to move find++; if (find<pos-1 && nextnode!=this.head) {//before ending the Find node, move the "front node" current = Current.getnextnOde ();
}//System.out.println (Prenode);
System.out.println (NextNode);
2, delete node Prenode.setnextnode (nextnode); System.GC ();
Recycle Delete node this.size--;
Flag = true;
return flag; * * * Specify the node pos for the linked list, modify the corresponding node, subscript starting from 1 * */@Override public boolean updatenode (int pos, map<string, object>
MAP) {Boolean flag = false; Snode node = getnode (pos, map);
Obtain the corresponding position POS node if (node!=null) {String data = (String) map.get ("Data");
Node.setdata (data);
Flag = true;
return flag;
* * * Find node POS for specified list, subscript starting from 1 */@Override public snode getnode (int pos, map<string, object> Map) {
Snode current = This.head.getNextNode ();
if (pos<=0 | | pos>this.size | | current==this.head) {SYSTEM.OUT.PRINTLN ("Location information error or list does not exist");
return null;
int find = 0; while (find<pos-1 && current!=this.head) {current = CURrent.getnextnode ();
find++;
return to current;
/* * Print List */@Override public void Printlink () {int length = This.size; if (length==0) {System.out.println ("The linked list is empty!")
");
return;
} Snode current = This.head.getNextNode ();
System.out.println ("A total number of nodes:" + length + "");
int find = 0;
while (Current!=this.head) {System.out.println ("first" + (++find) + "node:" + current);
Current=current.getnextnode ();
} public static void Main (string[] args) {singlecyclelinklist scll = new Singlecyclelinklist ();
Snode Node1 = new Snode ("Node 1");
Snode Node2 = new Snode ("Node 2");
Snode node3 = new Snode ("Node 3");
Snode node4 = new Snode ("Node 4");
Snode node5 = new Snode ("Node 5");
Snode node6 = new Snode ("Insert specified position");
Scll.insertposnode (Scll.getsize () +1, Node1);
Scll.insertposnode (Scll.getsize () +1, node2);
Scll.insertposnode (Scll.getsize () +1, node3); Scll.insertposnoDe (Scll.getsize () +1, node4);
Scll.insertposnode (Scll.getsize () +1, NODE5);
Scll.insertnode (Node1);
Scll.insertnode (Node2);
Scll.insertnode (NODE3);
Scll.insertnode (NODE4);
Scll.insertnode (NODE5);
System.out.println ("******************* Output chain table *******************");
Scll.printlink ();
System.out.println ("******************* Gets the specified list node *******************");
int pos = 2;
System.out.println ("Get the list" +pos+ "position data:" +scll.getnode (POS, null));
System.out.println ("******************* insert node to the list specified *******************");
int pos1 = 3;
System.out.println ("Insert data into the" +pos1+ "node:");
Scll.insertposnode (POS1, node6);
Scll.printlink ();
System.out.println ("******************* Delete a list specify location node *******************");
int pos2 = 3;
System.out.println ("Delete the first" +pos2+ "node:");
Scll.deletenode (POS2);
Scll.printlink ();
SYSTEM.OUT.PRINTLN ("******************* modify the linked list to specify the location node *******************");
int POS3 = 3; System.Out.println ("Modify the first" +pos3+ "node:");
map<string, object> map = new hashmap<> ();
Map.put ("Data", "This is a Test");
Scll.updatenode (POS3, map);
Scll.printlink ();
}
}
Two-linked list of cyclic lists
Package linklisttest;
Import Java.util.HashMap;
Import Java.util.Map; public class Doublecyclelinklist implements icommoperate<dnode>{private Dnode Header = new Dnode ("Head"); After the declaration does not change the private int size = 0;
Record list node number public int GetSize () {return this.size; * * * * List inserts, each to the end of the insertion, determine whether the end of the standard is next point head * */@Override public boolean Insertnode (Dnode node) {Boolean
Flag = false; Initlinklist ();
Initialize the linked list Dnode current = This.head;
if (this.size==0) {//Empty link Table this.head.setNextNode (node);
Node.setpriornode (This.head);
Node.setnextnode (This.head); }else{//Link inside node while (Current.getnextnode ()!=this.head) {//Find end node current = Current.getnextnode ()
;
} current.setnextnode (node);
Node.setpriornode (current); Node.setnextnode (This.head);
Follow bad linked list, tail node point head} this.size++;
Flag = true;
return flag; * * * INSERT list at specified POS, starting from 1, and POS largeTo size is inserted at the end of the list * */@Override public boolean insertposnode (int pos, Dnode node) {Boolean flag = true; Initlinklist ();
Initialize the linked list Dnode current = This.head.getNextNode ();
if (this.size==0) {//linked list is empty this.head.setNextNode (node);
Node.setpriornode (This.head);
Node.setnextnode (This.head);
this.size++;
}else if (pos>this.size) {//POS position is greater than the chain table length, insert end insertnode (node);
}else if (pos>0 && pos<=this.size) {//linked list of nodes//1, locate POS node to insert position, insert POS node current position int find = 0;
while (Find<pos-1 && current.getnextnode ()!=this.head) {current = Current.getnextnode ();
find++;
}//2, insert node if (Current.getnextnode () ==this.head) {//Tail node Node.setpriornode (current);
Node.setnextnode (This.head);
Current.setnextnode (node); else if (Current.getnextnode ()!=this.head) {//Intermediate node Node.setpriornode (Current.getpriornode ());
Node.setnextnode (current);
Current.getpriornode (). Setnextnode (node);
Current.setpriornode (node);
} this.size++;
}else{System.out.println ("Position information error");
Flag = false;
return flag;
private void Initlinklist () {if (size==0) {this.head.setNextNode (this.head);
This.head.setPriorNode (This.head); }/* Specifies the node POS for the linked list and deletes the corresponding node.
Way: Find to delete node before and after node deletion, subscript starting from 1 * */@Override public boolean deletenode (int pos) {Boolean flag = false;
Dnode current = This.head.getNextNode ();
if (pos<=0 | | pos>this.size | | current==this.head) {SYSTEM.OUT.PRINTLN ("Location information error or list does not exist");
}else{//1, locate the location to remove pos node int find = 0;
while (Find<pos-1 && current.getnextnode ()!=this.head) {current = Current.getnextnode ();
find++; }//2, delete node if (Current.getnextnode () ==this.head) {//Tail node Current.getpriornode (). Setnextnode (This.hea D ;
else if (Current.getnextnode ()!=this.head) {//Intermediate node Current.getpriornode (). Setnextnode (Current.getnextnode ());
Current.getnextnode (). Setpriornode (Current.getpriornode ()); } System.GC ();
Recycle Delete node this.size--;
Flag = true;
return flag; * * * Specify the node pos for the linked list, modify the corresponding node, subscript starting from 1 * */@Override public boolean updatenode (int pos, map<string, object>
MAP) {Boolean flag = false;
Dnode node = getnode (pos, map);
if (node!=null) {String data = (string) map.get ("Data");
Node.setdata (data);
Flag = true;
return flag;
* * * Find node POS for specified list, subscript starting from 1 */@Override public dnode getnode (int pos, map<string, object> Map) {
Dnode current = This.head.getNextNode ();
if (pos<=0 | | pos>this.size | | current==this.head) {SYSTEM.OUT.PRINTLN ("Location information error or list does not exist");
return null;
int find = 0; while (Find<pos-1 && current!=this.Head) {current = Current.getnextnode ();
find++;
return to current;
/* * Print List */@Override public void Printlink () {int length = This.size; if (length==0) {System.out.println ("The linked list is empty!")
");
return;
} Dnode current = This.head.getNextNode ();
int find = 0;
System.out.println ("A total number of nodes:" + length + "");
while (Current!=this.head) {System.out.println ("first" + (++find) + "node:" + current);
Current=current.getnextnode ();
} public static void Main (string[] args) {doublecyclelinklist dcll = new Doublecyclelinklist ();
Dnode Node1 = new Dnode ("Node 1");
Dnode Node2 = new Dnode ("Node 2");
Dnode node3 = new Dnode ("Node 3");
Dnode node4 = new Dnode ("Node 4");
Dnode node5 = new Dnode ("Node 5");
Dnode node6 = new Dnode ("Insert specified position");
Dcll.insertposnode (Node1);
Dcll.insertposnode (Node2);
Dcll.insertposnode (8, NODE3);
Dcll.insertposnode (NODE4); Dcll. Insertposnode (8, NODE5);
Dcll.insertnode (Node1);
Dcll.insertnode (Node2);
Dcll.insertnode (NODE3);
Dcll.insertnode (NODE4);
Dcll.insertnode (NODE5);
System.out.println ("******************* Output chain table *******************");
Dcll.printlink ();
System.out.println ("******************* Gets the specified list node *******************");
int pos = 2;
System.out.println ("Get the list" +pos+ "position data:" +dcll.getnode (POS, null));
System.out.println ("******************* insert node to the list specified *******************");
int pos1 = Dcll.getsize () +1;
System.out.println ("Insert data into the" +pos1+ "node:");
Dcll.insertposnode (POS1, node6);
Dcll.printlink ();
System.out.println ("******************* Delete a list specify location node *******************");
int pos2 = 7;
System.out.println ("Delete the first" +pos2+ "node:");
Dcll.deletenode (POS2);
Dcll.printlink ();
SYSTEM.OUT.PRINTLN ("******************* modify the linked list to specify the location node *******************");
int POS3 = 3; System.out.println ("Modify the first" +pos3+ "node:");
map<string, object> map = new hashmap<> ();
Map.put ("Data", "This is a Test");
Dcll.updatenode (POS3, map);
Dcll.printlink ();
}
}
Thank you for reading, I hope to help you, thank you for your support for this site!