1. Two-way linked list: In the single linked list of each node, and then set a pointer to its predecessor node field, then the two-way linked list nodes have two pointer fields, one point direct successor, the other point direct precursor.
2. Single linked list and two-way linked list comparison:
Single linked list: Always to find the node from beginning to end, can only be traversed, not back traversal.
Two-way linked list: You can find the tail from scratch, you can also find the head from the tail, that is, positive and negative traversal can be effective to improve the time performance of the algorithm, but because each node needs to record two pointers, so in space occupy a little more, this is through space to change time.
3. Java implementation bidirectional linked list:
Bidirectional linked list public class Doublelinkedlist<e> {private node<e> head;//Header node private int size;//Linked list
Length//Set up an empty list of public doublelinkedlist () {head = NULL;
size = 0; Insert public boolean addbeforehead (E data) {node<e> NewNode = new node<e& before header node
gt; (data);
if (IsEmpty ()) {head = NewNode;
}else{Head.setprev (NewNode);
Newnode.setnext (head);
head = NewNode;
} size++;
return true; ////Insert public boolean addaftertail (E data) {node<e> NewNode = new NODE<E&G at the end of the list
t; (data);
if (IsEmpty ()) {head = NewNode;
}else{node<e> tail = get (size);
Tail.setnext (NewNode); Newnode.setprev (tail);
Set up the last node of the new node} size++;
return true; }
Inserts the node at the specified location public boolean insert (int position, E data) {if (Position >= 1 && Pos
ition <= size + 1) {node<e> NewNode = new node<e> (data);
if (IsEmpty () | | | position = = 1) {//The linked list is empty or inserts the Addbeforehead (data) before the head node;
}else if (position = = size + 1) {//insert node<e> Prenode = Get (position-1) after the end node;
Newnode.setprev (Prenode);
Prenode.setnext (NewNode); }else{//Inserts node<e> Prenode = Get (position-1) in another location;//Gets the previous node position T e> Afternode = Prenode.getnext (); Gets the position position corresponding to the node Newnode.setprev (prenode) when the node is not inserted; ①newnode.setnext (Afternode); ②afternode.setprev (NewNode); ③prenode.setnext (NewNode);
④} size++;
return true;
} return false;
//Deletes the node at the specified location public E Delete (int position) {E result = null; if (position >= 1 && position <= size) {if (position = 1) {//delete head node result =
Head.getdata ();
node<e> afterhead = Head.getnext ();
Afterhead.setprev (NULL);
Head.setnext (NULL);
head = Afterhead;
}else if (position = = size) {//delete tail node node<e> Prenode = Get (position-1);//Obtain the previous node of the node to be deleted node<e> Delnode = Prenode.getnext ();
Gets the node to be deleted result = Delnode.getdata ();
Prenode.setnext (NULL); }else{//Delete other nodes node<e> Prenode = Get (position-1);//Obtain the previous node of the node to be deleted node<e& Gt Delnode = Prenode.getnext ();
Gets the node to be deleted result = Delnode.getdata (); Node<e> NextNode = Delnode.getnext ()//Get the next node of the node to be deleted Prenode.setnext (nextnode); ①nextnode.setprev (Prenode);
②} size--;
return result; //Gets the node (positive-sequence traversal) of a location public node<e> get (int position) {Node<e> TargetNode = n
ull;
if (!isempty () && position >= 1 && position <= size) {TargetNode = head;
for (int i = 1; i < position i++) {TargetNode = Targetnode.getnext ();//loop to get the node of the corresponding position
} return TargetNode;
//Get the length of the list of public int getsize () {return size;
//Determine if the list is empty public boolean isempty () {return size = = 0;
///Print List data public void display () {node<e> Node = head;
System.out.print ("Two-way linked list:"); for (int i = 0; i < size; i++) {System.out.print ("" + Node.getdata ());
node = Node.getnext ();
} System.out.println (""); }
}
More Highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
//Node class, data with nodes and references to the next node public class Node<e> {private E data;//field Private node<e> NEX T The pointer field holds the reference to the next node private node<e> prev;
The pointer field holds the reference to the previous node (this pointer is more than a list of doubly linked lists) public node () {} public node (E data) {
This.data = data;
Public Node (E data, node<e> Next, node<e> prev) {this.data = data;
This.next = Next;
This.prev = prev;
Public E GetData () {return data;
public void SetData (E data) {this.data = data;
Public node<e> GetNext () {return next;
public void Setnext (node<e> next) {This.next = next;
Public node<e> GetPrev () {return prev;
public void Setprev (node<e> prev) {this.prev = prev; }
}
public class Main {public
static void Main (string[] args) {
doublelinkedlist<integer> dll = new Doublelinke Dlist<integer> ();
Dll.addbeforehead (2);
Dll.addaftertail (3);
Dll.addbeforehead (1);
Dll.display ();
Dll.insert (4,4);
Dll.insert (5,5);
Dll.insert (6,6);
Dll.display ();
Dll.delete (6);
Dll.delete (3);
Dll.delete (1);
Dll.display ();
System.out.println ("The length of the two-way linked list is: " + dll.getsize ());
}
Author: csdn Blog zdp072