fork download
  1. class SinglyLinkedList {
  2.  
  3. // Node class to represent each node in the linked list
  4. class Node {
  5. int data;
  6. Node next;
  7.  
  8. // Constructor to create a new node
  9. public Node(int data) {
  10. this.data = data;
  11. this.next = null;
  12. }
  13. }
  14.  
  15. private Node head; // head of the list
  16.  
  17. // Constructor to initialize an empty linked list
  18. public SinglyLinkedList() {
  19. head = null;
  20. }
  21.  
  22. // Insert a node at the end of the linked list
  23. public void insert(int data) {
  24. Node newNode = new Node(data);
  25. if (head == null) {
  26. head = newNode;
  27. return;
  28. }
  29.  
  30. // Traverse to the last node
  31. Node last = head;
  32. while (last.next != null) {
  33. last = last.next;
  34. }
  35.  
  36. last.next = newNode; // Insert new node at the end
  37. }
  38.  
  39. // Delete a node by key
  40. public void delete(int key) {
  41. Node temp = head;
  42. Node prev = null;
  43.  
  44. // If the node to be deleted is the head node
  45. if (temp != null && temp.data == key) {
  46. head = temp.next; // Move head to the next node
  47. temp = null;
  48. return;
  49. }
  50.  
  51. // Search for the node to be deleted
  52. while (temp != null && temp.data != key) {
  53. prev = temp;
  54. temp = temp.next;
  55. }
  56.  
  57. // If the key was not found
  58. if (temp == null) {
  59. System.out.println("Node with data " + key + " not found.");
  60. return;
  61. }
  62.  
  63. // Unlink the node from the linked list
  64. prev.next = temp.next;
  65. temp = null;
  66. }
  67.  
  68. // Search for a node with a specific value
  69. public boolean search(int key) {
  70. Node temp = head;
  71. while (temp != null) {
  72. if (temp.data == key) {
  73. return true;
  74. }
  75. temp = temp.next;
  76. }
  77. return false;
  78. }
  79.  
  80. // Display the linked list
  81. public void display() {
  82. if (head == null) {
  83. System.out.println("List is empty.");
  84. return;
  85. }
  86.  
  87. Node temp = head;
  88. while (temp != null) {
  89. System.out.print(temp.data + " -> ");
  90. temp = temp.next;
  91. }
  92. System.out.println("null");
  93. }
  94.  
  95. // Main method to test the operations
  96. public static void main(String[] args) {
  97. SinglyLinkedList list = new SinglyLinkedList();
  98.  
  99. // Insert elements into the list
  100. list.insert(10);
  101. list.insert(20);
  102. list.insert(30);
  103. list.insert(40);
  104.  
  105. System.out.println("Linked List after insertions:");
  106. list.display();
  107.  
  108. // Searching for an element
  109. int keyToSearch = 20;
  110. if (list.search(keyToSearch)) {
  111. System.out.println("Node with data " + keyToSearch + " found!");
  112. } else {
  113. System.out.println("Node with data " + keyToSearch + " not found.");
  114. }
  115.  
  116. // Deleting an element
  117. int keyToDelete = 20;
  118. list.delete(keyToDelete);
  119. System.out.println("Linked List after deleting " + keyToDelete + ":");
  120. list.display();
  121.  
  122. // Trying to delete a non-existing element
  123. list.delete(50);
  124.  
  125. // Searching for a non-existing element
  126. if (list.search(50)) {
  127. System.out.println("Node with data 50 found!");
  128. } else {
  129. System.out.println("Node with data 50 not found.");
  130. }
  131. }
  132. }
  133.  
Success #stdin #stdout 0.12s 57536KB
stdin
Standard input is empty
stdout
Linked List after insertions:
10 -> 20 -> 30 -> 40 -> null
Node with data 20 found!
Linked List after deleting 20:
10 -> 30 -> 40 -> null
Node with data 50 not found.
Node with data 50 not found.