-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList_JAEKWANG97.java
More file actions
95 lines (82 loc) · 1.92 KB
/
Copy pathLinkedList_JAEKWANG97.java
File metadata and controls
95 lines (82 loc) · 1.92 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
package List.LinkedList;
import List.List;
public class LinkedList_JAEKWANG97<E> implements List<E> {
static class Node<E> {
E data;
Node<E> next;
Node<E> prev;
public Node(E data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
private Node<E> head;
private Node<E> tail;
private int size;
public LinkedList_JAEKWANG97() {
this.head = null;
this.tail = null;
this.size = 0;
}
@Override
public void insert(E data) {
Node<E> newNode = new Node<>(data);
if (head == null) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
}
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean contains(E o) {
if (isEmpty()) {
return false;
}
Node<E> cur = head;
while (cur != null) {
if (cur.data == o) {
return true;
}
cur = cur.next;
}
return false;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node<E> cur = head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur.data;
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
Node<E> cur = head;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
size--;
return cur.data;
}
}