Skip to content

Commit 8bbd619

Browse files
AdministratorAdministrator
Administrator
authored and
Administrator
committed
123
1 parent fe6ccfb commit 8bbd619

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>coding2017</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 109 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,131 @@
11
package com.coding.basic;
22

3+
import com.coding.basic.LinkedList.Node;
4+
35
public class LinkedList implements List {
46

57
private Node head;
8+
private int size;
69

710
public void add(Object o){
8-
11+
if (head.data == null) {
12+
head.data = o;
13+
head.next = null;
14+
size++;
15+
return;
16+
}
17+
Node node = new Node(o);
18+
Node curr = head;
19+
while (curr.next != null) {
20+
curr = curr.next;
21+
}
22+
curr.next = node;
23+
size++;
924
}
1025
public void add(int index , Object o){
11-
26+
if (index < 0 || index > size) {
27+
System.out.println(index + "无效指数");
28+
return;
29+
}
30+
if (head.data == null) {
31+
if (index == 0) {
32+
head.data = o;
33+
head.next = null;
34+
size++;
35+
return;
36+
} else {
37+
System.out.println("无效指数");
38+
return;
39+
}
40+
}
41+
Node node = new Node(o);
42+
Node curr = head;
43+
for (int i = 0; i < index - 1; i++) {
44+
curr = curr.next;
45+
}
46+
Node temp = curr.next;
47+
curr.next = node;
48+
node.next = temp;
49+
size++;
1250
}
1351
public Object get(int index){
14-
return null;
52+
if (index < 0 || index > size) {
53+
System.out.println(index + " is invalid index!");
54+
return null;
55+
}
56+
Node result = head;
57+
for (int i = 0; i < index; i++) {
58+
result = result.next;
59+
}
60+
return result;
1561
}
1662
public Object remove(int index){
17-
return null;
63+
if (index < 0 || index > size) {
64+
System.out.println(index + " is invalid index!");
65+
return null;
66+
}
67+
Node curr = head;
68+
for (int i = 0; i < index - 1; i++) {
69+
curr = curr.next;
70+
}
71+
Node result = curr.next;
72+
curr.next = curr.next.next;
73+
size--;
74+
return result;
1875
}
1976

2077
public int size(){
21-
return -1;
78+
return this.size;
2279
}
2380

2481
public void addFirst(Object o){
25-
82+
if (head.data == null) {
83+
head.data = o;
84+
head.next = null;
85+
size++;
86+
return;
87+
}
88+
Node temp = head;
89+
head = new Node(o);
90+
head.next = temp;
91+
size++;
2692
}
2793
public void addLast(Object o){
28-
94+
if (head.data == null) {
95+
head.data = o;
96+
head.next = null;
97+
size++;
98+
return;
99+
}
100+
Node node = new Node(o);
101+
Node curr = head;
102+
while (curr.next != null) {
103+
curr = curr.next;
104+
}
105+
curr.next = node;
106+
size++;
29107
}
30108
public Object removeFirst(){
31-
return null;
109+
if (head.data == null) {
110+
return null;
111+
}
112+
Node result = head;
113+
head = head.next;
114+
size--;
115+
return result;
32116
}
33117
public Object removeLast(){
34-
return null;
118+
if (head.data == null) {
119+
return null;
120+
}
121+
Node curr = head;
122+
for (int i = 0; i < size - 1; i++) {
123+
curr = curr.next;
124+
}
125+
Node result = curr.next;
126+
curr.next = null;
127+
size--;
128+
return result;
35129
}
36130
public Iterator iterator(){
37131
return null;
@@ -41,6 +135,12 @@ public Iterator iterator(){
41135
private static class Node{
42136
Object data;
43137
Node next;
138+
public Node(Object o) {
139+
data = o;
140+
next = null;
141+
}
142+
143+
44144

45145
}
46146
}

0 commit comments

Comments
 (0)