-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Introduction to doubly linked list
- Loading branch information
1 parent
f40f631
commit f27db58
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//Introduction to doubly linked list | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class Node { | ||
int data; | ||
Node next; | ||
Node prev; | ||
|
||
Node(int x) { | ||
data = x; | ||
next = null; | ||
prev= null; | ||
} | ||
} | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
|
||
while(t-- > 0) { | ||
int n = sc.nextInt(); | ||
int[] arr =new int[n]; | ||
|
||
for(int i = 0; i < n; i++) | ||
arr[i] = sc.nextInt(); | ||
|
||
Solution obj = new Solution(); | ||
Node ans = obj.constructDLL(arr); | ||
|
||
while(ans != null) { | ||
System.out.print(ans.data + " "); | ||
ans = ans.next; | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
|
||
class Solution { | ||
Node constructDLL(int arr[]) { | ||
Node head = new Node(arr[0]); | ||
Node temp = head; | ||
|
||
for(int i = 1; i < arr.length; i++) { | ||
Node newNode = new Node(arr[i]); | ||
newNode.next = null; | ||
newNode.prev = temp; | ||
temp.next = newNode; | ||
temp = newNode; | ||
} | ||
|
||
return head; | ||
} | ||
} |