Skip to content

Commit 25ef1af

Browse files
committed
LinkedList implementation onlyliuxin#1
1 parent 182865b commit 25ef1af

File tree

1 file changed

+94
-30
lines changed

1 file changed

+94
-30
lines changed
Lines changed: 94 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,117 @@
11
package com.basic.datastructure;
22

3-
public class LinkedList implements List{
3+
public class LinkedList implements List {
44
private Node first;
55
private Node last;
6-
6+
77
private int size;
8-
9-
public void add(Object o){
10-
if(first == null){
11-
first = new Node(o,null);
12-
}
13-
size ++;
8+
9+
public void add(Object o) {
10+
addLast(o);
1411
}
15-
public void add(int index , Object o){
16-
12+
13+
public void add(int index, Object o) {
14+
checkRange(index);
15+
if (index == 0) {
16+
addFirst(o);
17+
} else if (index == size) {
18+
addLast(o);
19+
} else {
20+
21+
}
1722
}
18-
public Object get(int index){
23+
24+
public Object get(int index) {
25+
checkRange(index);
26+
if(size > 0){
27+
int loopTimes = 0;
28+
Node p = first;
29+
while(index > loopTimes){
30+
p = p.next;
31+
loopTimes ++;
32+
}
33+
return p;
34+
}
35+
1936
return null;
2037
}
21-
public Object remove(int index){
38+
39+
public Object remove(int index) {
2240
return null;
2341
}
24-
25-
public int size(){
42+
43+
public int size() {
2644
return size;
2745
}
28-
29-
public void addFirst(Object o){
30-
46+
47+
private void addFirst(Object o) {
48+
if (size == 0) {
49+
first = new Node(o);
50+
last = first;
51+
} else {
52+
Node tmpNode = new Node(o);
53+
tmpNode.next = first;
54+
first = tmpNode;
55+
}
56+
size++;
3157
}
32-
public void addLast(Object o){
33-
58+
59+
private void addLast(Object o) {
60+
if (size == 0) {
61+
first = new Node(o);
62+
last = first;
63+
} else {
64+
last.next = new Node(o);
65+
last = last.next;
66+
}
67+
size++;
3468
}
35-
public Object removeFirst(){
36-
return null;
69+
70+
public Object removeFirst() {
71+
Node tmpNode = first;
72+
first = first.next;
73+
size--;
74+
return tmpNode;
3775
}
38-
public Object removeLast(){
76+
77+
public Object removeLast() {
78+
Node tmpNode = last;
79+
3980
return null;
4081
}
41-
42-
43-
private static class Node{
44-
Object item;
45-
Node next;
46-
Node(Object item, Node next){
82+
83+
private void checkRange(int index) {
84+
if (index > size || index < 0) {
85+
throw new IndexOutOfBoundsException("Index: " + index + "Size: " + size);
86+
}
87+
}
88+
89+
public String toString() {
90+
StringBuilder sb = new StringBuilder();
91+
Node p = first;
92+
while (p != null) {
93+
sb.append(p.item + "\n");
94+
p = p.next;
95+
}
96+
return sb.toString();
97+
}
98+
99+
private static class Node {
100+
private Object item;
101+
private Node next;
102+
103+
Node(Object item) {
47104
this.item = item;
48-
this.next = next;
49105
}
50106
}
51107

52-
}
108+
public static void main(String[] args) {
109+
LinkedList list = new LinkedList();
110+
for (int i = 0; i < 5; i++) {
111+
list.add(i);
112+
}
113+
list.get(5);
53114

115+
System.out.println(list);
116+
}
117+
}

0 commit comments

Comments
 (0)