-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
139 lines (123 loc) · 3.7 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
139 lines (123 loc) · 3.7 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package dsaCodes;
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class SinglyLinkedList {
private Node head;
public SinglyLinkedList() {
this.head = null;
}
// Insertion at the beginning
public void insertAtBeginning(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
// Insertion at the end
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = newNode;
}
// Insertion at a specific position
public void insertAtPosition(int position, int data) {
if (position == 0) {
insertAtBeginning(data);
return;
}
Node newNode = new Node(data);
Node current = head;
for (int i = 0; i < position - 1; i++) {
if (current == null) {
throw new IndexOutOfBoundsException("Position out of range");
}
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
// Deletion at the beginning
public void deleteFromBeginning() {
if (head == null) {
System.out.println("List is empty");
return;
}
head = head.next;
}
// Deletion at the end
public void deleteFromEnd() {
if (head == null) {
System.out.println("List is empty");
return;
}
if (head.next == null) {
head = null;
return;
}
Node secondLast = head;
while (secondLast.next.next != null) {
secondLast = secondLast.next;
}
secondLast.next = null;
}
// Deletion at a specific position
public void deleteFromPosition(int position) {
if (head == null) {
System.out.println("List is empty");
return;
}
if (position == 0) {
head = head.next;
return;
}
Node current = head;
for (int i = 0; i < position - 1; i++) {
if (current.next == null) {
throw new IndexOutOfBoundsException("Position out of range");
}
current = current.next;
}
if (current.next == null) {
throw new IndexOutOfBoundsException("Position out of range");
}
current.next = current.next.next;
}
// Utility function to print the list
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("None");
}
// Main method for testing
public static void main(String[] args) {
SinglyLinkedList sll = new SinglyLinkedList();
// Perform operations to test the linked list
sll.insertAtBeginning(3);
sll.insertAtEnd(5);
sll.insertAtPosition(1, 4);
sll.printList(); // Output: 3 -> 4 -> 5 -> None
sll.deleteFromBeginning();
sll.printList(); // Output: 4 -> 5 -> None
sll.deleteFromEnd();
sll.printList(); // Output: 4 -> None
sll.insertAtEnd(6);
sll.insertAtEnd(7);
sll.deleteFromPosition(1);
sll.printList(); // Output: 4 -> 7 -> None
}
}