Skip to content

Commit b794f47

Browse files
committed
added question.java
1 parent e4dffbe commit b794f47

File tree

6 files changed

+63
-72
lines changed

6 files changed

+63
-72
lines changed

Diff for: Linked-Lists/LinkedList.class

1.11 KB
Binary file not shown.

Diff for: Linked-Lists/LinkedList.java

-72
This file was deleted.

Diff for: Linked-Lists/LinkedListNode.class

1.08 KB
Binary file not shown.

Diff for: Linked-Lists/Node.class

288 Bytes
Binary file not shown.

Diff for: Linked-Lists/Question.class

768 Bytes
Binary file not shown.

Diff for: Linked-Lists/Question.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package Question2_3;
2+
3+
import java.util.*;
4+
// import CtCIDataStructurs.CtCIDataStructures.LinkedListNode;
5+
6+
public class Question {
7+
public Question() {}
8+
9+
public static void main(String[] args) {
10+
LinkedListNode first = new LinkedListNode(1, null, null);
11+
LinkedListNode root = first;
12+
for(int i = 2; i < 10; i++) {
13+
LinkedListNode sec = new LinkedListNode(i, null, null);
14+
first.setNext(sec);
15+
sec.setPrev(first);
16+
first = sec;
17+
}
18+
System.out.println(root.printNode());
19+
}
20+
}
21+
class LinkedListNode {
22+
int data;
23+
LinkedListNode next;
24+
LinkedListNode prev;
25+
26+
public LinkedListNode() {
27+
}
28+
29+
public LinkedListNode(int d, LinkedListNode n, LinkedListNode p) {
30+
data = d;
31+
this.setNext(n);
32+
this.setPrev(p);
33+
}
34+
//sets the next node
35+
public void setNext(LinkedListNode n) {
36+
next = n;
37+
if(n != null && n.prev != this) { //n.prev is checked in order stop adding the node recursively
38+
n.setPrev(this);
39+
}
40+
}
41+
//sets the prev node
42+
public void setPrev(LinkedListNode p) {
43+
prev = p;
44+
if(p != null && p.next != this) { //p.next is to imply termination
45+
this.setNext(p);
46+
}
47+
}
48+
49+
public String printNode() {
50+
if(next != null) {
51+
return data + ":" + next.printNode();
52+
}
53+
else
54+
return ((Integer) data).toString();
55+
}
56+
57+
// public static void main(String[] args) {
58+
// LinkedListNode first = new LinkedListNode(10, null, null);
59+
// LinkedListNode sec = new LinkedListNode(20, null, null);
60+
// first.setNext(sec);
61+
// System.out.println(first.printNode());
62+
// }
63+
}

0 commit comments

Comments
 (0)