1
+ // Java Class for Doubly Linked List
2
+ class Doubly_linkedList
3
+ {
4
+ Node head ; // list head
5
+
6
+ /* Doubly Linked list Node*/
7
+ class Node
8
+ {
9
+ int data ;
10
+ Node prev ;
11
+ Node next ;
12
+
13
+ //create a new node using constructor
14
+ Node (int d ) { data = d ;
15
+ }
16
+ }
17
+
18
+ // insert a node at the front of the list
19
+ public void insert_front (int new_data )
20
+ {
21
+ /* 1. allocate node
22
+ * 2. put in the data */
23
+ Node new_Node = new Node (new_data );
24
+
25
+ /* 3. Make next of new node as head and previous as NULL */
26
+ new_Node .next = head ;
27
+ new_Node .prev = null ;
28
+
29
+ /* 4. change prev of head node to new node */
30
+ if (head != null )
31
+ head .prev = new_Node ;
32
+
33
+ /* 5. move the head to point to the new node */
34
+ head = new_Node ;
35
+ }
36
+ //insert a node after the given prev node
37
+ public void Insert_After (Node prev_Node , int new_data )
38
+ {
39
+ //check that prev node is not null
40
+ if (prev_Node == null )
41
+ {
42
+ System .out .println ("The previous node is required,it cannot be NULL " );
43
+ return ;
44
+ }
45
+
46
+ //allocate new node and set it to data
47
+ Node newNode = new Node (new_data );
48
+
49
+ //set next of newNode as next of prev node
50
+ newNode .next = prev_Node .next ;
51
+
52
+ //set new node to next of prev node
53
+ prev_Node .next = newNode ;
54
+
55
+ //set prev of newNode as prev node
56
+ newNode .prev = prev_Node ;
57
+
58
+ //set prev of new node's next to newnode
59
+ if (newNode .next != null )
60
+ newNode .next .prev = newNode ;
61
+ }
62
+
63
+ // Add a node at the end of the list
64
+ void insert_end (int new_data )
65
+ {
66
+ //allocate the node and set the data
67
+ Node newNode = new Node (new_data );
68
+ Node last = head ; //set last as the head
69
+ //set next of new node to null since its the last node
70
+ newNode .next = null ;
71
+
72
+ //set new node as head if the list is null
73
+ if (head == null )
74
+ {
75
+ newNode .prev = null ;
76
+ head = newNode ;
77
+ return ;
78
+ }
79
+ //if list is not null then traverse it till the last node and set last next to last
80
+ while (last .next != null )
81
+ last = last .next ;
82
+
83
+ last .next = newNode ; //set last next to new node
84
+
85
+ newNode .prev = last ; //set last as prev of new node
86
+ }
87
+ // display the contents of linked list starting from the given node
88
+ public void displaylist (Node node )
89
+ {
90
+ Node last = null ;
91
+ while (node != null )
92
+ {
93
+ System .out .print (node .data + "<==>" );
94
+ last = node ;
95
+ node = node .next ;
96
+ }
97
+ if (node == null )
98
+ System .out .print ("null" );
99
+ System .out .println ();
100
+
101
+ }
102
+ }
103
+ class Main {
104
+ public static void main (String [] args )
105
+ {
106
+ /* Start with the empty list */
107
+ Doubly_linkedList dll = new Doubly_linkedList ();
108
+
109
+ // Insert 40.
110
+ dll .insert_end (40 );
111
+
112
+ // Insert 20 at the beginning.
113
+ dll .insert_front (20 );
114
+
115
+ // Insert 10 at the beginning.
116
+ dll .insert_front (10 );
117
+
118
+ // Insert 50 at the end.
119
+ dll .insert_end (50 );
120
+
121
+ // Insert 30, after 20.
122
+ dll .Insert_After (dll .head .next , 30 );
123
+
124
+ System .out .println ("Doubly linked list created is as follows: " );
125
+ dll .displaylist (dll .head );
126
+ }
127
+ }
0 commit comments