-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLLAnyType (1).java
More file actions
173 lines (146 loc) · 4.2 KB
/
SinglyLLAnyType (1).java
File metadata and controls
173 lines (146 loc) · 4.2 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
173
class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
this.next = null;
}
}
class LinkedListADT<T> {
private Node<T> head;
public LinkedListADT() {
this.head = null;
}
public boolean isEmpty() {
return head == null;
}
public void insertAtFront(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
} else {
newNode.next = head;
head = newNode;
}
}
public void insertAtEnd(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
} else {
Node<T> current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
public void insertAtIndex(T data, int index) {
if (index < 0 || index > getSize()) {
System.out.println("Invalid index");
return;
}
if (index == 0) {
insertAtFront(data);
return;
}
Node<T> newNode = new Node<>(data);
Node<T> current = head;
int count = 0;
while (count < index - 1) {
current = current.next;
count++;
}
newNode.next = current.next;
current.next = newNode;
}
public void deleteAtFront() {
if (isEmpty()) {
System.out.println("Linked list is empty");
} else {
head = head.next;
}
}
public void deleteAtEnd() {
if (isEmpty()) {
System.out.println("Linked list is empty");
} else if (head.next == null) {
head = null;
} else {
Node<T> current = head;
Node<T> previous = null;
while (current.next != null) {
previous = current;
current = current.next;
}
previous.next = null;
}
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= getSize()) {
System.out.println("Invalid index");
return;
}
if (index == 0) {
deleteAtFront();
return;
}
Node<T> current = head;
int count = 0;
while (count < index - 1) {
current = current.next;
count++;
}
current.next = current.next.next;
}
public boolean search(T data) {
Node<T> current = head;
while (current != null) {
if (current.data.equals(data)) {
return true;
}
current = current.next;
}
return false;
}
public int getSize() {
int count = 0;
Node<T> current = head;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public void display() {
if (isEmpty()) {
System.out.println("Linked list is empty");
} else {
Node<T> current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
}
}
public class MainAnyType {
public static void main(String[] args) {
LinkedListADT<Integer> integerList = new LinkedListADT<>();
integerList.insertAtFront(3);
integerList.insertAtFront(2);
integerList.insertAtFront(1);
integerList.display(); // Output: 1 2 3
LinkedListADT<String> stringList = new LinkedListADT<>();
stringList.insertAtEnd("World");
stringList.insertAtFront("Hello");
stringList.display(); // Output: Hello World
LinkedListADT<Object> anyList = new LinkedListADT<>();
anyList.insertAtEnd('N');
anyList.insertAtIndex('R', 1);
anyList.insertAtIndex(10, 2);
anyList.insertAtFront("Hello");
anyList.display(); // Output: Hello N R 10
}
}