Skip to content

Commit

Permalink
Create Introduction to doubly linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
dishathakurata authored May 14, 2024
1 parent f40f631 commit f27db58
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Introduction to doubly linked list
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;
}
}

0 comments on commit f27db58

Please sign in to comment.