JAVA Program to Delete a Node of a Circular Linked List
A Circular Linked List is collection of data elements where each element points towards the next element and the last Element points towards the First Element Hence forming a Loop. Below on this page we will learn about Circular Linked List Deletion in java.
Algorithm for deletion from beginning of a circular linked list
import java.util.*;
public class Main {
public static void main(String[] args) {
Main Obj = new Main();
Obj.add(10);
Obj.add(20);
Obj.add(30);
Obj.add(40);
Obj.add(50);
Obj.add(60);
System.out.println("List Before Deletion");
Obj.print();
System.out.println("List After Deleting first node");
Obj.deleteFirst();
Obj.print();
System.out.println("List After Deleting last node");
Obj.deleteLast();
Obj.print();
Obj.deleteNthNode(2);
System.out.println("List After Deleting 2nd node");
Obj.print();
}
public class Node{
int element;
Node next;
public Node(int element) {
this.element = element;
}
}
public Node head = null;
public Node tail = null;
public void print() {
Node temp = head;
if(head == null) {
System.out.println("null");
}
else {
do{
System.out.print(" "+ temp.element);
temp = temp.next;
}while(temp != head);
System.out.println();
}
}
public void add(int element){
Node newNode = new Node(element);
if(head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
}
else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}
public void deleteFirst() {
if(head == null) {
return;
}
else {
if(head != tail ) {
head = head.next;
tail.next = head;
}
else {
head = tail = null;
}
}
}
public void deleteLast() {
if(head == null) {
return;
}
else {
if(head != tail ) {
Node current = head;
while(current.next != tail) {
current = current.next;
}
tail = current;
tail.next = head;
}
else {
head = tail = null;
}
}
}
public int calcLen(){
int len = 0;
Node temp=head;
while(temp!=tail){
temp = temp.next;
len++;
}
return len;
}
public void deleteNthNode(int n)
{
int len = calcLen();
// Can only insert after 1st position
// Can't insert if position to insert is greater than size of Linked List
if(n < 1 || n > len)
{
System.out.println("Can't delete\n");
}
else
{
if(n == 1)
{
head = head.next;
return;
}
// required to traverse
Node temp = head;
Node previous = null;
// traverse to the nth node
while(--n > 0) {
previous = temp;
temp = temp.next;
}
// assigned next node of the previous node to nth node's next
previous.next = temp.next;
System.out.println("Deleted: " + temp.element);
}
}
}
Output
List Before Deletion
10 20 30 40 50 60
List After Deleting first node
20 30 40 50 60
List After Deleting last node
20 30 40 50
Deleted: 30
List After Deleting 2nd node
20 40 50
import java.lang.*;
public class Main {
public static void main(String[] args) {
Main Obj = new Main();
Obj.add(10);
Obj.add(20);
Obj.add(30);
Obj.add(40);
Obj.add(50);
Obj.add(60);
System.out.println("List Before Deletion");
Obj.print();
System.out.println("List After Deleting first node");
Obj.deleteFirst();
Obj.print();
System.out.println("List After Deleting last node");
Obj.deleteLast();
Obj.print();
Obj.deletenth(2);
System.out.println("List After Deleting 2nd node");
Obj.print();
}
public class Node{
int element;
Node next;
Node prev;
public Node(int element) {
this.element = element;
}
}
public Node head = null;
public Node tail = null;
public void print() {
Node temp = head;
if(head == null) {
System.out.println("null");
}
else {
do{
System.out.print(" "+ temp.element);
temp = temp.next;
}while(temp != head);
System.out.println();
}
}
public void add(int element){
Node newNode = new Node(element);
if(head == null) {
head = newNode;
tail = newNode;
newNode.next = head;
newNode.prev=head;
}
else {
tail.next = newNode;
newNode.prev=tail;
tail = newNode;
tail.next = head;
}
}
public int Length(Node head)
{
Node current = head;
int count = 0;
// if list is empty
// simply return length zero
if (head == null)
{
return 0;
}
// traverse forst to last node
else
{
do
{
current = current.next;
count++;
} while (current != head);
}
return count;
}
public void deleteFirst() {
if(head == null) {
return;
}
else {
if(head != tail ) {
head = head.next;
tail.next = head;
}
else {
head = tail = null;
}
}
}
public void deleteLast() {
if(head == null) {
return;
}
else {
if(head != tail ) {
Node current = head;
while(current.next != tail) {
current = current.next;
}
tail = current;
tail.next = head;
head.prev=tail;
}
else {
head = tail = null;
}
}
}
public void deletenth (int n)
{
if (head == null)
{
return;
}
else
{
Node current = head;
int pos = n;
for (int i = 1; i < pos; i++)
{
current = current.next;
}
if (current == head)
{
head = current.next;
}
else if (current == null)
{
current = current.prev;
}
else
{
current.prev.next = current.next;
current.next.prev = current.prev;
}
//Delete the middle node
current = null;
}
}
void printList ()
{
//Node current will point to head
Node curr = head;
if (head == null)
{
System.out.println ("List is empty");
return;
}
while (curr != null)
{
//Prints each node by increasing order of the pointer
System.out.print (curr.element + " ");
curr = curr.next;
}
System.out.println ();
}
}
Output
List Before Deletion
10 20 30 40 50 60
List After Deleting first node
20 30 40 50 60
List After Deleting last node
20 30 40 50
List After Deleting 2nd node
20 40 50
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment