-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List.java
More file actions
172 lines (154 loc) · 5.04 KB
/
Copy pathLinked_List.java
File metadata and controls
172 lines (154 loc) · 5.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
public class Linked_List {
public static void main(String[] args) {
linked_list list = new linked_list();
// Inserting at beginning
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtBeginning(30);
System.out.println("After insertAtBeginning:");
list.display();
//Reversal of the Linked List
list.reversal();
list.display();
// Inserting at end
list.insertAtEnd(99);
System.out.println("\nAfter insertAtEnd:");
list.display();
// Inserting at position
list.insertAtPosition(25, 2); // Should insert between 30 and 20
System.out.println("\nAfter insertAtPosition (25 at index 2):");
list.display();
// Deleting from beginning
list.deleteFromBeginning();
System.out.println("\nAfter deleteFromBeginning:");
list.display();
// Deleting from end
list.deleteFromEnd();
System.out.println("\nAfter deleteFromEnd:");
list.display();
// Deleting from position
list.deleteFromPosition(1); // Should delete node at index 1
System.out.println("\nAfter deleteFromPosition (index 1):");
list.display();
// Display final size
int size = list.list_size();
System.out.println("\nFinal size of list: " + size);
}
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public static class linked_list {
Node head;
public void insertAtBeginning(int data) {
Node created_node = new Node(data);
created_node.next = head;
head = created_node;
}
public void insertAtEnd(int data) {
Node created_node = new Node(data);
if (head == null) {
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = created_node;
}
public void insertAtPosition(int data, int position) {
Node created_node = new Node(data);
Node temp = head;
for (int i = 0; i < position; i++) {
temp = temp.next;
}
created_node.next = temp.next;
temp.next = created_node;
}
public void deleteFromBeginning() {
if (head == null) {
System.out.println("You cannot delete from an Empty Linked List");
return;
}
Node temp = head;
head = head.next;
temp.next = null;
}
public void deleteFromEnd() {
Node temp = head;
if (temp.next == null) {
head = null;
return;
}
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
public void deleteFromPosition(int position) {
Node temp = head;
if(position < 0 || head == null){
System.out.println("Error in given position or list is empty");
return;
}
if(position == 0){
head = head.next;
return;
}
for (int i = 0; i < position - 1; i++) {
if (temp.next == null) {
System.out.println("Position out of bounds");
return;
}
temp = temp.next;
}
if (temp.next == null) {
System.out.println("No node exists at your given position");
return;
}
Node deleted = temp.next;
temp.next = temp.next.next;
deleted.next = null;
}
public void display(){
Node temp = head;
if(temp == null){
System.out.println("The Linked List is empty");
return;
}
do {
System.out.print(temp.data + " -> ");
temp = temp.next;
} while (temp != null);
System.out.print("null\n");
}
public int list_size(){
if(head == null){
return 0;
}
int counter = 0;
Node temp = head;
do {
temp = temp.next;
counter++;
} while(temp != null);
return counter;
}
public void reversal(){
Node temp = head;
Node prev = null;
Node curr = temp;
while(curr != null){
Node next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head = prev;
}
}
}