Skip to content

Commit a42e407

Browse files
author
张加涛
committed
LinkedList Completed
1 parent f385948 commit a42e407

File tree

1 file changed

+128
-12
lines changed

1 file changed

+128
-12
lines changed
Lines changed: 128 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,174 @@
11
package com.coding.basic;
22

3+
import java.util.Objects;
4+
35
/**
46
* Created by zhangjiatao on 2017/2/25.
57
*/
68
public class LinkedList implements List {
7-
private Node head;
9+
private Node head = null,tail = null;
10+
private int size=0;
11+
private LinkedListIterator iterator = null;
812

913
@Override
1014
public void add(Object o) {
11-
15+
Node newNode = new Node(o);
16+
if(tail == null) {
17+
head = tail = newNode;
18+
}else {
19+
tail.next = newNode;
20+
tail = newNode;
21+
}
22+
size++;
1223
}
1324

1425
@Override
1526
public void add(int index, Object o) {
16-
27+
ifOutOfBounds(index);
28+
Node temp = head,previousNode = head;
29+
Node newNode = new Node(o);
30+
if(index == 0) {
31+
newNode.next = head;
32+
head = newNode;
33+
return ;
34+
}
35+
while(index-- > 0) {
36+
previousNode = temp;
37+
temp = temp.next;
38+
}
39+
previousNode.next = newNode;
40+
newNode.next = temp;
41+
size++;
1742
}
1843

1944
@Override
2045
public Object get(int index) {
21-
return null;
46+
ifOutOfBounds(index);
47+
Node temp = head;
48+
while(index-- > 0) {
49+
temp = temp.next;
50+
}
51+
return temp;
2252
}
2353

2454
@Override
2555
public Object remove(int index) {
26-
return null;
56+
ifOutOfBounds(index);
57+
Node temp = head,previousNode = head;
58+
if(head == tail) {
59+
head = tail = null;
60+
return temp;
61+
}
62+
while(index-- > 0) {
63+
previousNode = temp;
64+
temp = temp.next;
65+
}
66+
if(tail == temp) {
67+
tail = previousNode;
68+
}
69+
previousNode.next = temp.next;
70+
size--;
71+
return temp;
2772
}
2873

2974
@Override
3075
public int size() {
31-
return 0;
76+
return size;
3277
}
3378

3479
public void addFirst(Object o) {
35-
80+
add(0,o);
3681
}
3782

3883
public void addLast(Object o) {
39-
84+
add(o);
4085
}
4186

4287
public Object removeFirst() {
43-
return null;
88+
return remove(0);
4489
}
4590

4691
public Object removeLast() {
47-
return null;
92+
return remove(size()-1);
93+
}
94+
95+
public LinkedListIterator Iterator() {
96+
if(iterator ==null) {
97+
iterator = new LinkedListIterator(this);
98+
}
99+
return iterator;
48100
}
49101

50-
public Iterator iterator() {
51-
return null;
102+
private void ifOutOfBounds(int index) {
103+
if(index >= size || index < 0) {
104+
throw new IndexOutOfBoundsException();
105+
}
106+
}
107+
108+
private boolean isEmpty() {
109+
return size==0;
110+
}
111+
112+
public String toString() {
113+
String str = "";
114+
Node temp = head;
115+
while(temp!=tail) {
116+
str = str + temp.data + " ";
117+
temp = temp.next;
118+
}
119+
return str + tail.data;
52120
}
53121

54122
private static class Node {
55123
Object data;
56124
Node next;
125+
public Node(Object o) {
126+
this.data = o;
127+
this.next = null;
128+
}
129+
}
130+
131+
private class LinkedListIterator implements Iterator {
132+
private LinkedList linkedList = null;
133+
private Node currentNode = null;
134+
135+
public LinkedListIterator(LinkedList linkedList) {
136+
this.linkedList = linkedList;
137+
this.currentNode = this.linkedList.head;
138+
}
139+
140+
@Override
141+
public boolean hasNext() {
142+
if(linkedList.isEmpty()) return false;
143+
return currentNode.next != null;
144+
}
145+
146+
@Override
147+
public Object next() {
148+
Object data = currentNode.data;
149+
if(hasNext()) {
150+
currentNode = currentNode.next;
151+
}
152+
return data;
153+
}
154+
}
155+
156+
public static void main(String[] args) {
157+
LinkedList arr = new LinkedList();
158+
arr.add(1);
159+
arr.add(2);
160+
arr.add(3);
161+
arr.add(5);
162+
arr.add(1,4);
163+
arr.remove(2);
164+
arr.removeLast();
165+
arr.addFirst(5);
166+
System.out.println(arr.toString());
167+
System.out.println(arr.size());
168+
System.out.println(arr.Iterator().hasNext());
169+
System.out.println(arr.Iterator().next());
170+
System.out.println(arr.Iterator().next());
171+
System.out.println(arr.Iterator().next());
172+
System.out.println(arr.Iterator().next());
57173
}
58174
}

0 commit comments

Comments
 (0)