-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeletion of Single Link List at Specific Location
More file actions
124 lines (97 loc) · 3.83 KB
/
Deletion of Single Link List at Specific Location
File metadata and controls
124 lines (97 loc) · 3.83 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
package DataS;
// Define the Node class
// Define the Deletion_Single_Link_Specific_Location class
public class Deletion_Single_Link_Specific_Location {
private Node head; // Head of the linked list
class Node {
int data;
Node next;
// Constructor to initialize a new node with given data
public Node(int data) {
this.data = data;
this.next = null;
}
}
// Constructor to initialize an empty linked list
public Deletion_Single_Link_Specific_Location() {
this.head = null;
}
// Method to insert a node at a specific location in the linked list
public void insertAtLocation(int data, int position) {
Node newNode = new Node(data); // Create a new node with the given data
// If inserting at the head position
if (position == 0) {
newNode.next = head;
head = newNode;
return;
}
Node current = head;
int currentPosition = 0;
// Traverse to the node just before the position
while (currentPosition < position - 1 && current != null) {
current = current.next;
currentPosition++;
}
// If position is out of bounds
if (current == null) {
System.out.println("Invalid position. Inserting at the end.");
return;
}
// Insert the new node at the specified position
newNode.next = current.next;
current.next = newNode;
}
// Method to delete a node at a specific location in the linked list
public void deleteAtLocation(int position) {
// If the list is empty, nothing to delete
if (head == null) {
System.out.println("List is empty. Nothing to delete.");
return;
}
// If deleting the head node
if (position == 0) {
head = head.next;
return;
}
Node current = head;
int currentPosition = 0;
// Traverse to the node just before the position
while (currentPosition < position - 1 && current != null) {
current = current.next;
currentPosition++;
}
// If position is out of bounds
if (current == null || current.next == null) {
System.out.println("Invalid position. No node found to delete.");
return;
}
// Skip the node at the specified position
current.next = current.next.next;
}
// Method to print the linked list
public void printList() {
Node current = head; // Start from the head node
System.out.print("Linked List: ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Main method to test the implementation
public static void main(String[] args) {
Deletion_Single_Link_Specific_Location list = new Deletion_Single_Link_Specific_Location(); // Create an instance of Deletion_Single_Link_Specific_Location
// Insert nodes at specific locations
list.insertAtLocation(1, 0); // Insert 1 at position 0 (head)
list.insertAtLocation(2, 1); // Insert 2 at position 1
list.insertAtLocation(3, 1); // Insert 3 at position 1
// Print the linked list
System.out.println("Linked List after insertions:");
list.printList(); // Output: Linked List: 1 3 2
// Delete node at specific locations
list.deleteAtLocation(1); // Delete node at position 1
// Print the linked list after deletions
System.out.println("Linked List after deletions:");
list.printList(); // Output: Linked List: 3
}
}