-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday26.java
More file actions
192 lines (160 loc) · 4.44 KB
/
day26.java
File metadata and controls
192 lines (160 loc) · 4.44 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//ques1:Doubly linked list Insertion at given position
//link:https://www.geeksforgeeks.org/problems/insert-a-node-in-doubly-linked-list/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=insert-a-node-in-doubly-linked-list
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
next = prev = null;
}
}
class DLinkedList {
Node newNode(Node head, int data) {
Node n = new Node(data);
if (head == null) {
head = n;
return head;
}
head.next = n;
n.prev = head;
head = n;
return head;
}
void printList(Node node) {
Node temp = node;
while (temp.next != null) {
temp = temp.next;
}
while (temp.prev != null) {
temp = temp.prev;
}
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String args[]) throws IOException {
DLinkedList DLL = new DLinkedList();
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t > 0) {
Node temp;
Node head = null;
Node root = null;
String str[] = read.readLine().trim().split(" ");
int n = str.length;
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(str[i]);
head = DLL.newNode(head, x);
if (root == null) root = head;
}
head = root;
String str2[] = read.readLine().trim().split(" ");
int pos = Integer.parseInt(str2[0]);
int data = Integer.parseInt(str2[1]);
Solution g = new Solution();
head = g.addNode(head, pos, data);
DLL.printList(head);
while (head.next != null) {
temp = head;
head = head.next;
}
t--;
System.out.println("~");
}
}
}
// } Driver Code Ends
/* Structure of Doubly Linked List
class Node
{
int data;
Node next;
Node prev;
Node(int data)
{
this.data = data;
next = prev = null;
}
}*/
class Solution {
// Function to insert a new node at given position in doubly linked list.
Node addNode(Node head, int p, int x) {
// Your code here
Node nn= new Node(x);
Node temp=head;
if(head==null){
return nn;
}
for(int i=0;i<p && temp.next!=null;i++){
temp=temp.next;
}
nn.next=temp.next;
temp.next=nn;
nn.prev=temp;
if (nn.next != null) {
nn.next.prev = nn;
}
return head;
}
}
//ques2:Search in Linked List
//link:https://www.geeksforgeeks.org/problems/search-in-linked-list-1664434326/1?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=search-in-linked-list-1664434326
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = 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();
Node head = new Node(sc.nextInt());
Node tail = head;
for (int i = 0; i < n - 1; i++) {
tail.next = new Node(sc.nextInt());
tail = tail.next;
}
int key = sc.nextInt();
Solution g = new Solution();
boolean ans = g.searchKey(n, head, key);
System.out.println(ans ? "true" : "false");
// printList(head);
t--;
System.out.println("~");
}
}
}
// } Driver Code Ends
// User function Template for Java
/* Node of a linked list
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
*/
class Solution {
static boolean searchKey(int n, Node head, int key) {
Node temp = head;
while (temp != null) {
if (temp.data == key) return true;
temp = temp.next;
}
return false;
}
}