Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 100347a

Browse files
added CircularDoublyLinkedList
1 parent 9f7e002 commit 100347a

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package CircularDoublyLinkedList;
2+
3+
public class CircularDoublyLinkedList {
4+
private static class Node {
5+
int data;
6+
Node prev;
7+
Node next;
8+
9+
public Node(int data) {
10+
this.data = data;
11+
this.prev = null;
12+
this.next = null;
13+
}
14+
}
15+
16+
Node head;
17+
Node tail;
18+
int size = 0;
19+
20+
public void insert(int data) {
21+
Node node = new Node(data);
22+
if (head == null) {
23+
head = tail = node;
24+
} else {
25+
node.prev = tail;
26+
tail.next = node;
27+
tail = node;
28+
node.next = head;
29+
}
30+
size++;
31+
}
32+
33+
public void insertFirst(int data) {
34+
Node node = new Node(data);
35+
if (head == null) {
36+
head = tail = node;
37+
} else {
38+
node.next = head;
39+
node.prev = null;
40+
head = node;
41+
size++;
42+
}
43+
}
44+
45+
public void display() {
46+
Node n = head;
47+
while (n != null) {
48+
System.out.print(n.data + " -> ");
49+
n = n.next;
50+
}
51+
System.out.println("null");
52+
}
53+
54+
public static void main(String[] args) {
55+
CircularDoublyLinkedList cdll = new CircularDoublyLinkedList();
56+
cdll.insert(10);
57+
cdll.insert(20);
58+
cdll.insert(30);
59+
cdll.insert(40);
60+
cdll.insertFirst(5);
61+
cdll.display();
62+
}
63+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /