forked from IamBisrutPyne/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
117 lines (100 loc) · 2.98 KB
/
LinkedList.java
File metadata and controls
117 lines (100 loc) · 2.98 KB
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
/**
* Linked List Implementation in Java
*
* A Linked List is a linear data structure where elements (nodes)
* are connected using references (pointers). Each node contains:
* - data (value)
* - next (reference to next node)
*
* Advantages:
* - Dynamic size (no need to define capacity)
* - Easy insertion/deletion at any position
*
* Disadvantages:
* - Random access not possible (must traverse sequentially)
* - Uses extra memory (pointer/reference for each node)
*
* Example:
* Insert 10 → Insert 20 → Insert 30
* Linked List: 10 -> 20 -> 30
* Delete 20
* Linked List: 10 -> 30
*/
class LinkedList {
// Node class: represents a single element in Linked List
static class Node {
int data;
Node next;
Node(int data) {
this.data = data; // store value
this.next = null; // initially no link
}
}
Node head; // head (first node of the list)
// Insert new node at the end
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode; // if list is empty
} else {
Node temp = head;
while (temp.next != null) { // traverse till last node
temp = temp.next;
}
temp.next = newNode; // link new node at end
}
}
// Delete a node by value
public void delete(int key) {
Node temp = head, prev = null;
// If head itself holds the key
if (temp != null && temp.data == key) {
head = temp.next; // move head
return;
}
// Search for the key
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
// If key not found
if (temp == null) return;
// Unlink the node
prev.next = temp.next;
}
// Search an element
public boolean search(int key) {
Node temp = head;
while (temp != null) {
if (temp.data == key) return true;
temp = temp.next;
}
return false;
}
// Display the linked list
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("null");
}
// Main method to test LinkedList
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Insert elements
list.insert(10);
list.insert(20);
list.insert(30);
System.out.println("After Insertion:");
list.display(); // 10 -> 20 -> 30 -> null
// Delete element
list.delete(20);
System.out.println("After Deletion of 20:");
list.display(); // 10 -> 30 -> null
// Search element
System.out.println("Search 30: " + list.search(30)); // true
System.out.println("Search 50: " + list.search(50)); // false
}
}