-
Notifications
You must be signed in to change notification settings - Fork 0
/
LL.java
136 lines (116 loc) · 4.09 KB
/
LL.java
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
// Name = Movaliya Romil Rajeshbhai
// Enrollment no. 202103103510247
// Roll no. 25
// Practical no. 5 -- linkedlist
class LL{
public static class SinglyLinkedList {
private Node head;
private class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public void insertAtFront(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
public void insertInAscendingOrder(int data) {
Node newNode = new Node(data);
if (head == null || head.data >= data) {
newNode.next = head;
head = newNode;
return;
}
Node current = head;
while (current.next != null && current.next.data < data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
public void deleteFirst() {
if (head == null) {
return;
}
head = head.next;
}
public void deleteBefore(int pos) {
if (head == null || head.next == null || pos <= 2) {
deleteFirst();
return;
}
Node current = head;
int count = 1;
while (current.next.next != null && count < pos - 1) {
current = current.next;
count++;
}
current.next = current.next.next;
}
public void deleteAfter(int pos) {
if (head == null || head.next == null) {
return;
}
Node current = head;
int count = 1;
while (current.next != null && count < pos) {
current = current.next;
count++;
}
if (current.next != null) {
current.next = current.next.next;
}
}
public void display() {
if (head == null) {
System.out.println("The list is empty.");
return;
}
Node current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
// Inserting nodes at the front of the linked list
list.insertAtFront(5);
list.insertAtFront(10);
list.insertAtFront(15);
// Inserting nodes at the end of the linked list
list.insertAtEnd(20);
list.insertAtEnd(25);
list.insertAtEnd(30);
// Inserting nodes in ascending order
list.insertInAscendingOrder(18);
list.insertInAscendingOrder(8);
list.insertInAscendingOrder(28);
// Deleting the first node of the linked list
list.deleteFirst();
// Deleting the node before the specified position
list.deleteBefore(4);
// Deleting the node after the specified position
list.deleteAfter(3);
// Displaying the linked list
list.display();
}
}