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

Single Linked List

The document contains a Java implementation of a singly linked list, including methods for creating a list, inserting nodes, traversing the list, searching for a node, deleting a node, and deleting the entire list. The main method demonstrates the functionality of the linked list by creating a list, performing insertions, searching, and deletions. The implementation maintains a head, tail, and size for the linked list operations.

Uploaded by

bibek singh
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 views4 pages

Single Linked List

The document contains a Java implementation of a singly linked list, including methods for creating a list, inserting nodes, traversing the list, searching for a node, deleting a node, and deleting the entire list. The main method demonstrates the functionality of the linked list by creating a list, performing insertions, searching, and deletions. The implementation maintains a head, tail, and size for the linked list operations.

Uploaded by

bibek singh
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/ 4

package Linked_List;

public class SingleLinkedList {


public Node head;
public Node tail;
public int size;

public class Node {

public int value;


public Node next;

public Node createSingleLinkedList(int nodeValue) {


// head = new Node();
Node node = new Node();
node.next = null;
node.value = nodeValue;
head = node;
tail = node;
size = 1;
return head;
}

// Insert Method in SinglyLinkedList

public void InsertInLinkedList(int nodeValue, int location) {


Node node = new Node();
node.value = nodeValue;
if (head == null) {
createSingleLinkedList(nodeValue);
return;
} else if (location == 0) {
node.next = head;
head = node;
} else if (location >= size) {
node.next = null;
tail.next = node;
tail = node;
} else {
Node tempNode = head;
int index = 0;
while (index < location - 1) {
tempNode = tempNode.next;
index++;
}
Node nextNode = tempNode.next;
tempNode.next = node;
node.next = nextNode;

}
size++;
}

// Traversal of LinkedList
public void traverseSingleLinkedList() {
if (head == null) {
System.out.println("sll does not exit");
} else {
Node tempNode = head;
for (int i = 0; i < size; i++) {
System.out.print(tempNode.value);
if (i != size - 1) {
System.out.print(" -> ");
}
tempNode = tempNode.next;
}
}
System.out.println();
}

// Search for a node.TC=O(N),SC=O(1)


boolean searchNode(int nodeValue) {
if (head != null) {
Node tempNode = head;
for (int i = 0; i < size; i++) {
if (tempNode.value == nodeValue) {
System.out.println("found node at location: " + i);
return true;
}
tempNode = tempNode.next;
}
}
System.out.println("Node not found ");
return false;
}

// Deleting a node from SinglyLinkedList


public void deletionOfNode(int location) {
if (head == null) {
System.out.println("The single linked list does not exist");
return;
} else if (location == 0) {
head = head.next;
size--;
if (size == 0) {
tail = null;
}
} else if (location >= size) {
Node tempNode = head;
for (int i = 0; i < size - 1; i++) {
tempNode = tempNode.next;
}
if (tempNode == head) {
tail = head = null;
size--;
return;
}
tempNode.next = null;
tail = tempNode;
size--;
} else {
Node tempNode = head;
for (int i = 0; i < location - 1; i++) {
tempNode = tempNode.next;
}
tempNode.next = tempNode.next.next;
size--;
}
}

// Deleting entire single Linked list


public void deleteEntireList() {
head = tail = null;
}

public static void main(String[] args) {


// TODO Auto-generated method stub
SingleLinkedList sll = new SingleLinkedList();
sll.createSingleLinkedList(5);
// System.out.println(sll.head.value);
sll.InsertInLinkedList(6, 1);
// System.out.println(sll.head.next.value);
sll.InsertInLinkedList(7, 3);
// System.out.println(sll.head.next.next.value);
sll.InsertInLinkedList(8, 4);
sll.InsertInLinkedList(9, 0);
sll.traverseSingleLinkedList();
sll.searchNode(7);
sll.deletionOfNode(2);
sll.traverseSingleLinkedList();
sll.deleteEntireList();
sll.traverseSingleLinkedList();
}

OUTPUT:

You might also like