-
Notifications
You must be signed in to change notification settings - Fork 0
/
Creation_of_Doubly_Linked_List.java
66 lines (66 loc) · 1.59 KB
/
Creation_of_Doubly_Linked_List.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.Scanner;
class Node
{
int data;
Node next;
Node previous;
Node(int val)
{
this.data = val;
}
}
public class Creation_of_Double_Linked_List
{
static Node addElements(Node head,Node tail,int[] arr,int len)
{
for (int index=0;index<len;index++)
{
Node newNode = new Node(arr[index]);
if(head == null)
{
head = tail = newNode;
head.previous = null;
tail.next = null;
}
else
{
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
}
}
return head;
}
static void print(Node head)
{
Node curr = head;
if(head == null)
{
System.out.print("Empty List");
return;
}
while (curr != null)
{
System.out.print(curr.data);
System.out.print("->");
curr = curr.next;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int[] arr=new int[N];
for(int index=0;index<N;index++)
{
arr[index]=sc.nextInt();
}
Node head = null;
Node tail = null;
head = addElements(head,tail,arr,N);
System.out.println("Linked List Created is: ");
print(head);
sc.close();
}
}