Here you will get program to implement singly linked list in Java.
Linked list is a linear data structure. It contains different nodes. Each node will store data and reference to the next node. This is the most useful data structure in computer science in construction of trees especially.
Java Singly Linked List Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
import java.util.*; // we will create a node class first with data members class Node { int data; // node class should be self referential because it has to refer the next node Node next; //set data value, next node reference through method public Node(int d) { data=d; next=null; } } //Now we will create LinkedList class class Linkedlist{ Node head; //function to check whether linked list is empty or not public boolean isEmpty() { if(head==null) { return true; } else { return false; } } //function for inserting element in the begining public void insertionAtBegining(int val) { if(isEmpty()) { head=new Node(val); } else{ Node temp=new Node(val); temp.next=head; head=temp; } } //function for inserting at end public void insertionAtEnd(int val) { if(isEmpty()) { head=new Node(val); } else{ Node temp=head; while(temp.next!=null) { temp=temp.next; } temp.next=new Node(val); } } //function for insertion at specified position public void insertionAtPos(int val, int pos) { int count=1; Node temp=head; while(temp!=null) { count++; if(count==pos) { Node post = temp.next; temp.next = new Node(val); temp.next.next = post; } temp = temp.next; } if(count < pos) { System.out.println(count); System.out.println("linked list is in underflow to insert at specified position"); } } //function to traverse and print entire list public void printlist() { System.out.print("list is "); Node temp = head; while(temp != null) { System.out.print(" "+temp.data); temp=temp.next; } } //function to delete at first public void deleteAtBegining() { head = head.next; } // function to delete at specified postion public void deleteAtPos(int pos) { int count = 1; Node temp = head; while (temp != null) { count++; if(count == pos) { break; } temp=temp.next; } if(count < pos) { System.out.println("linked list is in underflow condition to delete in the position" +pos); } temp.next = temp.next.next; } // function to delete at end public void deleteAtEnd() { Node temp = head; while( temp.next.next != null ) { temp = temp.next; } temp.next = null; } // function to search for an element in the linked list public void search(int val) { Node temp = head; int count = 0; int flag = 0; while(temp != null) { count++; if(temp.data == val) { System.out.println( val + " found at pos " + count ); flag = 1; } temp = temp.next; } if(flag == 0) { System.out.println(val +" not found"); } } public static void main(String args[]) { Linkedlist listObj = new Linkedlist(); System.out.println("adding elements to linked list at begining:"); listObj.insertionAtBegining(4); System.out.println("4 is added in linked list"); listObj.printlist(); listObj.insertionAtBegining(5); System.out.println("\n5 is added in linked list"); listObj.printlist(); listObj.insertionAtBegining(7); System.out.println("\n7 is added in linked list"); listObj.printlist(); listObj.insertionAtBegining(10); System.out.println("\n10 is added in linked list"); listObj.printlist(); System.out.println("\n\nadding elements to linked list at end:"); listObj.insertionAtEnd(6); System.out.println("6 is added at the end of linked list"); listObj.insertionAtEnd(20); System.out.println("20 is added at the end of linked list"); listObj.insertionAtEnd(30); System.out.println("30 is added at the end of linked list"); listObj.printlist(); listObj.insertionAtPos(8,2); System.out.println("\n\n8 is added at the 2nd index of linked list"); listObj.printlist(); System.out.println("\n\ndeleted the first element"); listObj.deleteAtBegining(); listObj.printlist(); listObj.deleteAtPos(2); System.out.println("\ndeleted the element at pos 2"); listObj.printlist(); listObj.deleteAtEnd(); System.out.println("\ndeleted the element at end"); listObj.printlist(); System.out.println("\n\nsearching element 5 in list"); listObj.search(8); System.out.println("\nsearching element 15 in list"); listObj.search(15); } } |
Output
adding elements to linked list at begining:
4 is added in linked list
list is 4
5 is added in linked list
list is 5 4
7 is added in linked list
list is 7 5 4
10 is added in linked list
list is 10 7 5 4
adding elements to linked list at end:
6 is added at the end of linked list
20 is added at the end of linked list
30 is added at the end of linked list
list is 10 7 5 4 6 20 30
8 is added at the 2nd index of linked list
list is 10 8 7 5 4 6 20 30
deleted the first element
list is 8 7 5 4 6 20 30
deleted the element at pos 2
list is 8 5 4 6 20 30
deleted the element at end
list is 8 5 4 6 20
searching element 5 in list
8 found at pos 1
searching element 15 in list
15 not found
Comment below if you have any queries related to above java singly linked list program.