Way linked list of defects Management Analysis:
Singly linked list, the search direction is only one direction, and doubly linked list can look forward or backward .
Singly linked list can not delete itself, need to rely on the secondary node, and doubly linked list, you can delete itself , so the front of our list when deleting a single node, always find temp, temp before a node to be deleted node.
schematic diagram
The description of the FIG:
Analysis of the doubly linked list traversal, add, modify, delete the operation of ideas:
1) traversal : Method and as a single list, but can look forward, can also look back
2) Add :( added by default to the last two-way linked list)
- First find the last node in a doubly linked list
- temp.next = newHeroNode;
- newHeroNode.pre = temp;
3) Modify : ideas and original as singly linked list
4) Delete
- Because it is a doubly linked list, so you can achieve self-delete a node
- Direct you wish to delete this node, such as temp
- temp.pre.next = temp.next;
- temp.next.pre = temp.pre;
Code:
Category list node structure
// define HeroNode2, each object is a node HeroNode class HeroNode2 { public int NO; public String name; public String Nickname; public HeroNode2 Next; // to point to the next node, the default is null public HeroNode2 pre; // before a point node defaults to null // constructor public HeroNode2 ( int NO, String name, String Nickname) { the this .no = NO; the this .name = name; the this .nickname = Nickname; } //To display method, we re toString @Override public String toString () { return "HeroNode [NO =" + NO + ", name =" + name + ", Nickname =" Nickname + + "]" ; } }
Operation list category:
// create a doubly linked list class class DoubleLinkedList { // initialize a head node, the node not to move the head, does not store data specific Private HeroNode2 head = new new HeroNode2 (0, "", "" ); // return the head node public HeroNode2 the getHead () { return head; } // traversing a doubly linked list // display the list [traverse] public void List () { // determines whether the list is empty iF (head.next == null ) { the System.out .println ( "empty list" ); return ; } //Because the head node can not move, so we need an auxiliary variable to traverse HeroNode2 TEMP = head.next; the while ( to true ) { // determines whether or not the list to the last IF (TEMP == null ) { BREAK ; } // output node information System.out.println (temp); // after temp shift must be careful temp = temp.next; } } // . Add a node of a doubly linked list to the last public void the Add (HeroNode2 heroNode) { // because head node can not move, so we need an auxiliary traverse TEMP HeroNode2 TEMP = head; // traverse the list to find the last the while ( to true ) { // find the list of the final IF (temp.next == null ) { // BREAK ; } // if not found Finally, after the shift temp temp = temp.next ; } // when exit the while loop, TEMP points to the last of the list // form a doubly linked list temp.next = heroNode; heroNode.pre = TEMP; } // modify the contents of a node of a doubly linked list can be seen and modify the contents of the node singly linked list as // just node type to HeroNode2 public voidUpdate (HeroNode2 newHeroNode) { // determines whether an empty IF (head.next == null ) { System.out.println ( "list is empty ~" ); return ; } // find the need to modify the node, based on no number / / define an auxiliary variable HeroNode2 TEMP = head.next; Boolean In Flag = to false ; // indicating whether the node to find the while ( to true ) { iF (TEMP == null ) { BREAK ; // has been traversed list } IF (temp.no == newHeroNode.no) { // find flag = to true ; BREAK ; } TEMP = temp.next; } // The flag determines whether you want to modify the node IF (flag) { temp.name = newHeroNode.name; temp.nickname = newHeroNode.nickname; } the else { // not found System.out.printf ( "node number% d is not found, can not be modified \ n-" , newHeroNode.no); } } // remove a node from a doubly linked list, // Description // 1 for doubly linked lists, we can directly find you want to delete this node // After 2 find, delete itself to public void del ( int NO) { // determines whether the current list is empty iF (head.next == null ) { // empty list System.out.println ( "list is empty, can not be deleted" ); return ; } HeroNode2 TEMP = head.next; // auxiliary variable (pointer) boolean flag = false ; // flag is found to be deleted node of the while ( to true) { IF (TEMP == null ) { // the last to have the list BREAK ; } IF (temp.no == NO) { // the previous node of the deletion node to be found TEMP In Flag = to true ; BREAK ; } TEMP = TEMP .next; // the temp shift traversing } // Analyzing In Flag IF (In Flag) { // find // can delete // temp.next = temp.next.next; [way linked list] temp.pre.next =temp.next; // Here's the code we have a problem? // If it is the last node, you do not need to perform the following sentence, otherwise null pointer IF (temp.next =! null ) { temp.next.pre = temp.pre; } } the else { System.out.printf ( "% D node to be deleted does not exist \ n-" , nO); } } }