Package Linklist.doublelinklist;public class Node {public int data;//data domain public node next;//node field, next node public node prve;// node field, previous node//head node Initialize public node (node next) {This.data=data;this.next=next;this.prve=null;} Non-head node initialization public node (int data,node next,node prve) {this.data=data;this.next=next;this.prve=prve;}}
*************************************************************************************************************** *********************
Package Linklist.doublelinklist;public class Doublelinklist {node head;//head node int size;//list size Node current;//current node//Initialize empty chain. Table Public doublelinklist () {this.head = new Node (null); this.size = 0;this.current = head;} Determines whether the linear table is empty public boolean isEmpty () {return size = = 0;} Gets the element at the specified position, where we define the head node head at index -1public Node getelement (int index) {if (Index <-1 | | index >= size) {throw new Ru Ntimeexception ("parameter Error");} if (index = =-1) {current = head;} else {current = Head.next;} for (int i = 0; i < index && current.next! = null; i++) {current = Current.next;} return current;} Inserts the element at the specified position, where we define the head node head at index -1public void Insert (int index, int data) {node node = new Node (data, null,null); Node prev = getelement (index-1);//The previous node of the current node current=prev.next;//the current node if (current!=null) {node.prve=prev; Node.next=current; Current.prve=node; Prev.next=node;} Else{node.prve=prev; Node.next=current; Prev.next=node;} size++;} Deletes the element at the specified location public void Delete (int index) {if (Index > size -1) {throw new RuntimeException ("parameter Error");} Node prev = getelement (index-1);//The previous node of the current node is present = Prev.next;prev.next = Current.next;current.prve = prev;size--;} Print List public void traverse () {if (IsEmpty ()) {System.out.println ("null"),} else {current = Head.next;while (current! = Nu ll) {System.out.print (Current.data + "); current = Current.next;} System.out.println ();}}}
Java data structure System--linked list (3): Doubly linked list and related common operations