Skip to content

Instantly share code, notes, and snippets.

@z1nc0r3
Created August 27, 2022 12:16
Show Gist options
  • Save z1nc0r3/4ad2c4d9162dcd8ab74adcb93e95736c to your computer and use it in GitHub Desktop.
Save z1nc0r3/4ad2c4d9162dcd8ab74adcb93e95736c to your computer and use it in GitHub Desktop.
Circular Linked List
class Node {
int value;
Node next;
Node(int value) {
this.value = value;
next = null;
}
}
public class CircularLinkedList {
Node head = null;
Node current = null;
Node tail = null;
public void addNode(int value) {
Node newNode = new Node(value);
if (head == null) {
head = newNode;
} else {
tail.next = newNode;
}
tail = newNode;
tail.next = head;
}
public void insertAtBegin(int value) {
Node newNode = new Node(value);
current = head;
if (head == null) {
addNode(value);
} else {
newNode.next = head;
head = newNode;
tail.next = head;
}
}
public void insertAtEnd(int value) {
current = head;
if (head == null) {
addNode(value);
return;
}
tail.next = new Node(value);
tail = tail.next;
tail.next = head;
}
public void insertAt(int value, int position) {
Node newNode = new Node(value);
current = head;
if (position == 1) {
insertAtBegin(value);
} else {
for (int i = 1; i < position; i++) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
}
public void deleteAtBegin() {
if (head == null) {
System.out.println("Cannot delete from a empty list.");
return;
}
head = head.next;
tail.next = head;
}
public void deleteAtEnd() {
current = head;
if (head == null) {
System.out.println("Cannot delete from empty list.");
return;
}
while (current.next.next != head) {
current = current.next;
}
current.next = null;
tail = current;
tail.next = head;
}
public void deleteAt(int index) {
Node temp = head;
if (index == 1) {
deleteAtBegin();
return;
}
try {
for (int i=1; i<index; i++) {
temp = temp.next;
}
temp.next = temp.next.next;
tail = current;
tail.next = head;
} catch (Exception e) {
System.out.println("Index is out of bound.");
}
}
public void search(int value) {
Node temp = head;
int index = 1;
boolean found = false;
if (head == null) {
System.out.println("List is empty.");
return;
}
do {
if (temp.value == value) {
found = true;
break;
}
temp = temp.next;
index++;
} while (temp != head);
if (found) {
System.out.printf("Found at index %d. \n", index);
} else {
System.out.println("Not found in the list.");
}
}
public void print() {
Node temp = head;
if (temp == null) {
System.out.println("List is empty.");
return;
}
do {
System.out.print(temp.value + " ");
temp = temp.next;
}
while (temp != head);
System.out.println();
}
public static void main(String[] args) {
CircularLinkedList myList = new CircularLinkedList();
myList.addNode(5);
myList.addNode(1);
myList.addNode(3);
myList.addNode(8);
myList.addNode(2);
myList.print();
myList.insertAtBegin(9);
myList.print();
myList.insertAt(15, 4);
myList.print();
myList.insertAtEnd(10);
myList.print();
myList.deleteAtBegin();
myList.print();
myList.deleteAt(5);
myList.print();
myList.deleteAtEnd();
myList.print();
myList.deleteAt(15);
myList.search(4);
myList.search(9);
myList.print();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment