forked from yshshrm/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
175 lines (135 loc) · 3.71 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
175 lines (135 loc) · 3.71 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
174
175
public class SinglyLinkedList {
private class Node {
private Object value;
private Node next;
Node(final Object o) {
value = o;
next = null;
}
public Object getValue() {
return value;
}
public void setValue(final Object o) {
value = o;
}
public Node getNext() {
return next;
}
public void setNext(final Node n) {
next = n;
}
}
private Node head = null;
private int size = 0;
public void add(final int index, final Object element) {
if (index > size || index < 0) {
throw new RuntimeException("Invalid index.");
}
Node e = new Node(element);
if (index == 0) {
e.setNext(head);
head = e;
} else {
Node a = head;
for (int i = 0; i < index - 1; i++) {
a = a.getNext();
}
e.setNext(a.getNext());
a.setNext(e);
}
size++;
}
public void add(final Object element) {
Node e = new Node(element);
if (size == 0) {
head = e;
} else {
Node a = head;
while (a.getNext() != null) {
a = a.getNext();
}
a.setNext(e);
}
size++;
}
public Object get(final int index) {
if (index >= size || index < 0) {
throw new RuntimeException("Invalid index.");
}
Node a = head;
for (int i = 0; i < index; i++) {
a = a.getNext();
}
return a.getValue();
}
public void set(final int index, final Object element) {
if (index >= size || index < 0) {
throw new RuntimeException("Invalid index.");
}
Node a = head;
for (int i = 0; i < index; i++) {
a = a.getNext();
}
a.setValue(element);
}
public void clear() {
head = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public void remove(final int index) {
if (index >= size || index < 0) {
throw new RuntimeException("Invalid index.");
}
if (index == 0) {
head = head.getNext();
} else {
Node a = head;
for (int i = 0; i < index - 1; i++) {
a = a.getNext();
}
a.setNext(a.getNext().getNext());
}
size--;
}
public int size() {
return size;
}
public SinglyLinkedList sublist(final int fromIndex, final int toIndex) {
if (fromIndex < 0 || toIndex >= size || fromIndex > toIndex) {
throw new RuntimeException("Invalid index.");
}
SinglyLinkedList list = new SinglyLinkedList();
Node a = head;
int i;
for (i = 0; i < fromIndex; i++) {
a = a.getNext();
}
while (i <= toIndex) {
list.add(a.getValue());
a = a.getNext();
i++;
}
return list;
}
public boolean contains(final Object o) {
Node a = head;
while (a != null) {
if (a.getValue().equals(o)) {
return true;
}
a = a.getNext();
}
return false;
}
public static void main(String[] args) {
SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.add(3);
System.out.println(singlyLinkedList.contains(3));
System.out.println(singlyLinkedList.size());
singlyLinkedList.clear();
System.out.println(singlyLinkedList.size());
}
}