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

Linked List Operations in Java

The document provides Java implementations for three types of linked lists: Singly Linked List (SLL), Doubly Linked List (DLL), and Circular Linked List (CLL). Each linked list class includes methods for inserting and deleting nodes at various positions, as well as methods for printing the list in forward and backward order where applicable. The document serves as a comprehensive guide for performing common operations on linked lists in Java.

Uploaded by

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

Linked List Operations in Java

The document provides Java implementations for three types of linked lists: Singly Linked List (SLL), Doubly Linked List (DLL), and Circular Linked List (CLL). Each linked list class includes methods for inserting and deleting nodes at various positions, as well as methods for printing the list in forward and backward order where applicable. The document serves as a comprehensive guide for performing common operations on linked lists in Java.

Uploaded by

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

### Linked List Operations in Java

## Singly Linked List (SLL)


```java
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}

class SinglyLinkedList {
Node head;

public void insertAtHead(int data) {


Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}

public void insertAtTail(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}

public void insertAtPosition(int data, int position) {


if (position < 1) return;
if (position == 1) {
insertAtHead(data);
return;
}
Node newNode = new Node(data);
Node temp = head;
for (int i = 1; temp != null && i < position - 1; i++) {
temp = temp.next;
}
if (temp == null) return;
newNode.next = temp.next;
temp.next = newNode;
}
public void deleteFromHead() {
if (head == null) return;
head = head.next;
}

public void deleteFromTail() {


if (head == null || head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}

public void deleteAtPosition(int position) {


if (head == null || position < 1) return;
if (position == 1) {
deleteFromHead();
return;
}
Node temp = head;
for (int i = 1; temp != null && i < position - 1; i++) {
temp = temp.next;
}
if (temp == null || temp.next == null) return;
temp.next = temp.next.next;
}

public void printForward() {


Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
}
```

## Doubly Linked List (DLL)


```java
class NodeDLL {
int data;
NodeDLL next, prev;
public NodeDLL(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}

class DoublyLinkedList {
NodeDLL head, tail;

public void insertAtHead(int data) {


NodeDLL newNode = new NodeDLL(data);
if (head == null) {
head = tail = newNode;
} else {
newNode.next = head;
head.prev = newNode;
head = newNode;
}
}

public void insertAtTail(int data) {


NodeDLL newNode = new NodeDLL(data);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}

public void deleteFromHead() {


if (head == null) return;
if (head == tail) {
head = tail = null;
} else {
head = head.next;
head.prev = null;
}
}

public void deleteFromTail() {


if (tail == null) return;
if (head == tail) {
head = tail = null;
} else {
tail = tail.prev;
tail.next = null;
}
}

public void printForward() {


NodeDLL temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}

public void printBackward() {


NodeDLL temp = tail;
while (temp != null) {
System.out.print(temp.data + " <- ");
temp = temp.prev;
}
System.out.println("NULL");
}
}
```

## Circular Linked List (CLL)


```java
class NodeCLL {
int data;
NodeCLL next;
public NodeCLL(int data) {
this.data = data;
this.next = null;
}
}

class CircularLinkedList {
NodeCLL head, tail;

public void insertAtEnd(int data) {


NodeCLL newNode = new NodeCLL(data);
if (head == null) {
head = tail = newNode;
tail.next = head;
} else {
tail.next = newNode;
tail = newNode;
tail.next = head;
}
}

public void deleteFromHead() {


if (head == null) return;
if (head == tail) {
head = tail = null;
} else {
head = head.next;
tail.next = head;
}
}

public void printList() {


if (head == null) return;
NodeCLL temp = head;
do {
System.out.print(temp.data + " -> ");
temp = temp.next;
} while (temp != head);
System.out.println("HEAD");
}
}
```

This contains insertion, deletion, forward and backward traversal for all linked list types.

You might also like