-
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.
feat: Implement circular linked list and Add insert, delete and displ…
…ay methods
- Loading branch information
1 parent
0edff80
commit 6cf94be
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
JavaDsaWithTest/src/main/java/org/practice/dsa/linkedlist/CircularLinkedList.java
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,73 @@ | ||
package org.practice.dsa.linkedlist; | ||
|
||
public class CircularLinkedList { | ||
|
||
private Node head; | ||
private Node tail; | ||
|
||
public CircularLinkedList() { | ||
this.head = null; | ||
this.tail = null; | ||
} | ||
|
||
public void insert(int val) { | ||
Node node = new Node(val); | ||
if (head == null) { | ||
head = node; | ||
tail = node; | ||
return; | ||
} | ||
tail.next = node; | ||
node.next = head; | ||
tail = node; | ||
} | ||
|
||
public void display() { | ||
Node node = head; | ||
if (head != null) { | ||
do { | ||
System.out.print(node.val + "->"); | ||
if (node.next != null) { | ||
node = node.next; | ||
} | ||
} while (node != head); | ||
} | ||
System.out.println("HEAD"); | ||
} | ||
|
||
public void delete(int val) { | ||
Node node = head; | ||
if (node == null) { | ||
return; | ||
} | ||
|
||
if (head == tail) { | ||
head = null; | ||
tail = null; | ||
return; | ||
} | ||
|
||
if (node.val == val) { | ||
head = head.next; | ||
tail.next = head; | ||
} | ||
|
||
do { | ||
Node n = node.next; | ||
if (n.val == val) { | ||
node.next = n.next; | ||
break; | ||
} | ||
node = node.next; | ||
} while (node != head); | ||
} | ||
|
||
private class Node { | ||
int val; | ||
Node next; | ||
|
||
public Node(int val) { | ||
this.val = val; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
JavaDsaWithTest/src/main/java/org/practice/dsa/linkedlist/CircularMain.java
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,21 @@ | ||
package org.practice.dsa.linkedlist; | ||
|
||
public class CircularMain { | ||
public static void main(String[] args) { | ||
CircularLinkedList cll = new CircularLinkedList(); | ||
|
||
cll.insert(10); | ||
cll.insert(11); | ||
cll.insert(12); | ||
cll.insert(13); | ||
cll.insert(14); | ||
cll.insert(15); | ||
|
||
cll.display(); | ||
|
||
cll.delete(12); | ||
cll.delete(13); | ||
|
||
cll.display(); | ||
} | ||
} |