0% found this document useful (0 votes)
4 views5 pages

Circualr LinkedList Applications

The document provides a Java implementation of a circular linked list with methods to add, delete, and print nodes. It demonstrates adding nodes at both the beginning and end of the list, as well as deleting the first and last nodes. The main method showcases these functionalities through console output.

Uploaded by

ubaydahalesawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Circualr LinkedList Applications

The document provides a Java implementation of a circular linked list with methods to add, delete, and print nodes. It demonstrates adding nodes at both the beginning and end of the list, as well as deleting the first and last nodes. The main method showcases these functionalities through console output.

Uploaded by

ubaydahalesawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package LinkedCircularPackage;

public class App {

public static void main(String[] args) {


LLNode<Integer> circularList = null;

//How to add a new node at the begining of a circular linked list?


System.out.println("Adding a new node at the begining of a circular
linked list:");
System.out.println("Add: 8");
circularList = addFirst(circularList, 8);
System.out.println("Add: 3");
circularList = addFirst(circularList, 3);
System.out.println("Add: 5");
circularList = addFirst(circularList, 5);

//How to print all elements in a Circular linked list?


System.out.println("Printing all elements in a Circular linked list:");
printCircularList(circularList);

//How to identify the referenced node in a circular linked list?


System.out.println("circularList reference: " + circularList.getInfo());

//How to delete the first node from a circular linked list?


System.out.println("Delete the first node from a circular linked list:");
circularList = deleteFirst(circularList);
printCircularList(circularList);

// Reinitialize the circularList.


circularList = null;

//How to add a new node at the end of a circular linked list?


System.out.println("\nAdding a new node at the end of a circular linked
list:");
System.out.println("Add: 8");
circularList = addLast(circularList, 8);
System.out.println("Add: 3");
circularList = addLast(circularList, 3);
System.out.println("Add: 5");
circularList = addLast(circularList, 5);

//How to print all elements in a Circular linked list?


System.out.println("Printing all elements in a Circular linked list:");
printCircularList(circularList);

//How to identify the referenced node in a circular linked list?


System.out.println("circularList refrence: " + circularList.getInfo());

//How to delete the Last node from a circular linked list?


System.out.println("Delete the last node from a circular linked list:");
circularList = deleteLast(circularList);
printCircularList(circularList);

public static <T> void printCircularList(LLNode<T> list) {


if (list == null) {
System.out.println("It is an empty Circular linked list");
} else {
LLNode<T> temp = list.getLink();
System.out.println("List:");
while (temp != list) {
System.out.print(temp.getInfo() + "\t");
temp = temp.getLink();
}
System.out.println(temp.getInfo());
}
}

public static <T> LLNode<T> addFirst(LLNode<T> list, T value) {


LLNode<T> newNode = new LLNode<>(value);
if (list == null) {
list = newNode;
newNode.setLink(list);
} else {
newNode.setLink(list.getLink());
list.setLink(newNode);
}
return list;
}

public static <T> LLNode<T> addLast(LLNode<T> list, T value) {


LLNode<T> newNode = new LLNode<>(value);
if (list == null) {
list = newNode;
newNode.setLink(list);
} else {
newNode.setLink(list.getLink());
list.setLink(newNode);
list = newNode;
}
return list;
}

public static <T> LLNode<T> deleteFirst(LLNode<T> list) {


if (list == null || list.getLink() == list) {
list = null;
} else {
list.setLink(list.getLink().getLink());
}
return list;
}

public static <T> LLNode<T> deleteLast(LLNode<T> list) {


if (list == null || list.getLink() == list) {
list = null;
} else {
LLNode<T> temp = list.getLink();
while (temp.getLink() != list) {
temp = temp.getLink();
}
temp.setLink(list.getLink());
list = temp;
}
return list;
}

You might also like