public class Doublelinkedlist {public Node head;public doublelinkedlist () {head = new Node (); head.next = Null;head.prior = null;} The tail interpolation method public void Createbytail (int[] arr, int n) {node tail = head;for (int i=0; i<n; i++) {node c = new node (arr[i]); Tai L.next = C;c.prior = Tail;tail = C;} Tail.next = null;} Head interpolation public void Createbyhead (int[] arr, int n) {for (int i=0; i<n; i++) {Node c = new node (arr[i]); c.next = Head.next;c . Prior = Head;if (Head.next! = null) Head.next.prior = C;head.next = C;}} Insert a node public a Boolean insert (int i, int e) {int j=0; Node p = head;while (j<i-1 && p!=null) {j + +;p = P.next;} if (p = = null) {return false;} Node C = new node (e); c.next = P.next;c.prior = P;if (P.next! = null) {P.next.prior = C;} P.next = C;return true;} Delete a node public int[] Delete (int i) {int[] arr = new Int[2];int j=0; Node p = head;while (j<i-1 && p!=null) {j + +;p = P.next;} if (p = = NULL | | P.next = = null) {arr[0] = 0;return arr;} Arr[0] = 1;arr[1] = P.next.data;p.next = P.next.next;if(P.next!=null) p = p.next.prior; return arr;} public void display () {Node p = head.next;while (P! = null) {if (P.next! = null) System.out.print (P.data + "); else Syst Em.out.println (p.data);p = P.next;}} public static void Main (string[] args) {int[] arr = new int[]{5,6,1,6,9,3,7,8,10,11,4};D oublelinkedlist list = new Doublel Inkedlist (); List.createbyhead (arr, arr.length); List.display (); List.createbytail (arr, arr.length); List.display (); if (!list.insert) {System.out.println ("error"),} else {list.display ();} if (!list.insert) {System.out.println ("error");} else {list.display ();} int[] tmp = List.delete (n), if (tmp[0] = = 0) {System.out.println ("error"),} else {System.out.println ("delete" + tmp[1]); l Ist.display ();} TMP = List.delete (4), if (tmp[0] = = 0) {System.out.println ("error"),} else {System.out.println ("delete" + tmp[1]); List.display ();}}} class Node {public int data;public node Next;public node prior;public node () {}public node (int data) {This.data = Data;next = prior = null;}}
Results:
4->11->10->8->7->3->9->6->1->6->55->6->1->6->9->3->7->8- >10->11->4error5->6->1->6->9->3->7->8->10->99->11->4delete 45->6- >1->6->9->3->7->8->10->99->11delete 65->6->1->9->3->7->8->10- >99->11
Insert Delete for doubly linked list