-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
80 lines (65 loc) · 1.4 KB
/
LinkedList.java
File metadata and controls
80 lines (65 loc) · 1.4 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
package list;
public class LinkedList<T> {
private Node<T> head;
public Node<T> getHead() {
return this.head;
}
public void addAtStart(T data) {
Node<T> newNode = new Node<T>(data);
newNode.setNextNode(this.head);
this.head = newNode;
}
public void setHead(Node<T> data) {
this.head = data;
}
public Node<T> deleteAtStart() {
Node<T> toDel = this.head;
this.head = this.head.getNextNode();
return toDel;
}
public Node<T> find(T data) {
Node<T> curr = this.head;
while (curr != null) {
if (curr.getClass().equals(data)) {
return curr;
}
curr = curr.getNextNode();
}
return null;
}
public int length() {
if (head == null)
return 0;
int length = 0;
Node<T> curr = this.head;
while (curr != null) {
length += 1;
curr = curr.getNextNode();
}
return length;
}
public boolean isEmpty() {
return this.head == null;
}
@Override
public String toString() {
String res = "";
Node<T> curr = this.head;
while (curr != null) {
res += curr + ", ";
curr = curr.getNextNode();
}
return res;
}
public static void main(String[] args) {
LinkedList<Integer> integers = new LinkedList<Integer>();
integers.addAtStart(5);
integers.addAtStart(10);
integers.addAtStart(2);
integers.addAtStart(12);
integers.addAtStart(19);
integers.addAtStart(20);
System.out.println(integers.length());
System.out.println(integers.find(120));
}
}