Created
August 27, 2022 12:09
-
-
Save z1nc0r3/81dbb7b35991505f899d625a0d388198 to your computer and use it in GitHub Desktop.
Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
int value; | |
Node next; | |
Node(int value) { | |
this.value = value; | |
next = null; | |
} | |
} | |
public class SinglyLinkedList { | |
Node head = null; | |
Node current = null; | |
public void addNode(int value) { | |
Node newNode = new Node(value); | |
if (head == null) { | |
head = newNode; | |
} else { | |
current.next = newNode; | |
} | |
current = newNode; | |
} | |
public void insertAtBegin(int value) { | |
Node newNode = new Node(value); | |
if (head == null) { | |
addNode(value); | |
} else { | |
newNode.next = head; | |
head = newNode; | |
} | |
} | |
public void insertAtEnd(int value) { | |
current = head; | |
if (head == null) { | |
addNode(value); | |
return; | |
} | |
while (current.next != null) { | |
current = current.next; | |
} | |
current.next = new Node(value); | |
} | |
public void insertAt(int value, int position) { | |
Node newNode = new Node(value); | |
current = head; | |
if (position == 1) { | |
insertAtBegin(value); | |
} else { | |
for (int i=1; i<position; i++) { | |
current = current.next; | |
} | |
newNode.next = current.next; | |
current.next = newNode; | |
} | |
} | |
public void deleteAtBegin() { | |
if (head == null) { | |
System.out.println("Cannot delete from a empty list."); | |
return; | |
} | |
head = head.next; | |
} | |
public void deleteAtEnd() { | |
current = head; | |
if (head == null) { | |
System.out.println("Cannot delete from empty list."); | |
return; | |
} | |
while (current.next.next != null) { | |
current = current.next; | |
} | |
current.next = null; | |
} | |
public void deleteAt(int index) { | |
current = head; | |
if (index == 1) { | |
deleteAtBegin(); | |
return; | |
} | |
try { | |
for (int i=1; i<index; i++) { | |
current = current.next; | |
} | |
current.next = current.next.next; | |
} catch (Exception e) { | |
System.out.println("Index is out of bound."); | |
} | |
} | |
public void search(int value) { | |
current = head; | |
int index = 1; | |
boolean found = false; | |
if (head == null) { | |
System.out.println("List is empty."); | |
return; | |
} | |
while (current.next != null) { | |
if (current.value == value) { | |
found = true; | |
break; | |
} | |
current = current.next; | |
index++; | |
} | |
if (found) { | |
System.out.printf("Found at index %d. \n", index); | |
} else { | |
System.out.println("Not found in the list."); | |
} | |
} | |
public void print() { | |
current = head; | |
if (head == null) { | |
System.out.println("List is empty."); | |
return; | |
} | |
while (current != null) { | |
System.out.print(current.value + " "); | |
current = current.next; | |
} | |
System.out.println(); | |
} | |
public static void main(String[] args) { | |
SinglyLinkedList myList = new SinglyLinkedList(); | |
myList.addNode(5); | |
myList.addNode(1); | |
myList.addNode(3); | |
myList.addNode(8); | |
myList.addNode(2); | |
myList.print(); | |
myList.insertAtBegin(9); | |
myList.print(); | |
myList.insertAtEnd(9); | |
myList.print(); | |
myList.insertAt(15, 3); | |
myList.print(); | |
myList.deleteAtBegin(); | |
myList.print(); | |
myList.deleteAtEnd(); | |
myList.print(); | |
myList.deleteAt(2); | |
myList.print(); | |
myList.deleteAt(15); | |
myList.search(3); | |
myList.search(15); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment