Skip to content

Commit bca76d7

Browse files
authored
Merge pull request onlyliuxin#19 from xiongyilun/master
2-26作业
2 parents 05da6d2 + f4bcca7 commit bca76d7

File tree

12 files changed

+353
-24
lines changed

12 files changed

+353
-24
lines changed

group08/529757467/.classpath

Lines changed: 0 additions & 6 deletions
This file was deleted.

group08/529757467/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

group08/529757467/.project

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.coding.basic;
2+
3+
public class ArrayList implements List {
4+
// size不仅是ArrayList中实际存值得体现,直接add时,也是在elementData[size]处进行
5+
private int size = 0;
6+
// 数组是连续且有序的
7+
private Object[] elementData;
8+
9+
private void resize(int newSize) {
10+
Object[] newArray = new Object[newSize];
11+
System.arraycopy(elementData, 0, newArray, 0, size);
12+
elementData = newArray;
13+
}
14+
15+
public ArrayList() {
16+
elementData = new Object[10];
17+
}
18+
19+
// 添加指定元素至列表尾
20+
public void add(Object o) {
21+
if (size == elementData.length) {
22+
resize(size * 2);
23+
}
24+
elementData[size++] = o;
25+
}
26+
27+
// 将指定元素插入列表中的指定位置。移动当前位置的元素(如果有的话)和右边的后续元素(向索引添加一个元素)
28+
public void add(int index, Object o) {
29+
rangeCheck(index);
30+
if (size == elementData.length / 4) {
31+
resize(elementData.length / 2);
32+
}
33+
System.arraycopy(elementData, index, elementData, index + 1, size - index);
34+
elementData[index] = o;
35+
size++;
36+
}
37+
38+
/*
39+
* 获取指定位置元素
40+
* (non-Javadoc)
41+
* @see com.coding.basic.List#get(int)
42+
*/
43+
public Object get(int index) {
44+
rangeCheck(index);
45+
return elementData[index];
46+
}
47+
48+
/*
49+
* 删除指定位置元素
50+
* (non-Javadoc)
51+
* @see com.coding.basic.List#remove(int)
52+
*/
53+
public Object remove(int index) {
54+
rangeCheck(index);
55+
Object oldValue = elementData[index];
56+
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
57+
size--;
58+
return oldValue;
59+
}
60+
61+
public int size() {
62+
return this.size;
63+
}
64+
65+
public Iterator iterator() {
66+
return new ArrayListIterator();
67+
}
68+
69+
// 检测索引值,当前有size个元素,占了elementData[0]至elementData[size-1],add时,只能在已有[0,size-1]处插入,或者在列表尾size处add
70+
// 所以index在[0,size之间]
71+
private void rangeCheck(int index) {
72+
if (index < 0 || index > size) {
73+
throw new IndexOutOfBoundsException();
74+
}
75+
}
76+
77+
class ArrayListIterator implements Iterator {
78+
int i = 0;
79+
80+
@Override
81+
public boolean hasNext() {
82+
return i < size;
83+
}
84+
85+
@Override
86+
public Object next() {
87+
return elementData[i++];
88+
}
89+
90+
}
91+
92+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coding.basic;
2+
3+
public class BinaryTreeNode {
4+
5+
private Object data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
9+
public Object getData() {
10+
return data;
11+
}
12+
public void setData(Object data) {
13+
this.data = data;
14+
}
15+
public BinaryTreeNode getLeft() {
16+
return left;
17+
}
18+
public void setLeft(BinaryTreeNode left) {
19+
this.left = left;
20+
}
21+
public BinaryTreeNode getRight() {
22+
return right;
23+
}
24+
public void setRight(BinaryTreeNode right) {
25+
this.right = right;
26+
}
27+
28+
public BinaryTreeNode insert(Object o){
29+
return null;
30+
}
31+
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.coding.basic;
2+
3+
public class LinkedList implements List {
4+
5+
private int size = 0;
6+
// 头结点
7+
private Node head;
8+
// 尾结点
9+
private Node tail;
10+
11+
private void rangeCheck(int index) {
12+
if (index < 0 || index > size) {
13+
throw new IndexOutOfBoundsException();
14+
}
15+
}
16+
17+
public void add(Object o) {
18+
Node node = new Node(o, null);
19+
if (head == null) {
20+
head = tail = node;
21+
}
22+
Node oldTail = tail;
23+
tail = node;
24+
oldTail.next = tail;
25+
size++;
26+
}
27+
28+
public void add(int index, Object o) {
29+
rangeCheck(index);
30+
if (index == size) {
31+
this.add(o);
32+
} else {
33+
// 保存index处节点
34+
Node x = head;
35+
// 保存index-1处的节点
36+
Node y = null;
37+
for (int i = 0; i < index; i++) {
38+
y = x;
39+
x = x.next;
40+
}
41+
Node node = new Node(o, x);
42+
y.next = node;
43+
size++;
44+
}
45+
}
46+
47+
public Object get(int index) {
48+
rangeCheck(index);
49+
Node x = head;
50+
for (int i = 0; i < index; i++) {
51+
x = x.next;
52+
}
53+
return x.data;
54+
}
55+
56+
public Object remove(int index) {
57+
rangeCheck(index);
58+
Object removeData;
59+
if (index == 0) {
60+
removeData = removeFirst();
61+
} else if (index == size - 1) {
62+
removeData = removeLast();
63+
} else {
64+
Node x = head;
65+
Node y = head;
66+
for (int i = 0; i < index; i++) {
67+
y = x;
68+
x = x.next;
69+
}
70+
y.next = x.next;
71+
size--;
72+
removeData = x.data;
73+
}
74+
return removeData;
75+
}
76+
77+
public int size() {
78+
return this.size;
79+
}
80+
81+
public void addFirst(Object o) {
82+
Node oldHead = head;
83+
head = new Node(o, oldHead);
84+
size++;
85+
}
86+
87+
public void addLast(Object o) {
88+
Node oldTail = tail;
89+
tail = new Node(o, null);
90+
oldTail.next = tail;
91+
size++;
92+
}
93+
94+
public Object removeFirst() {
95+
Node oldHead = head;
96+
head = oldHead.next;
97+
size--;
98+
return oldHead.data;
99+
}
100+
101+
public Object removeLast() {
102+
Node oldTail = tail;
103+
Node temp = head;
104+
for (int i = 0; i < size - 2; i++) {
105+
temp = temp.next;
106+
}
107+
tail = temp;
108+
size--;
109+
return oldTail.data;
110+
}
111+
112+
public Iterator iterator() {
113+
return new LinkedListIterator();
114+
}
115+
116+
private static class Node {
117+
Object data;
118+
Node next;
119+
120+
Node(Object data, Node next) {
121+
this.data = data;
122+
this.next = next;
123+
}
124+
125+
}
126+
127+
private class LinkedListIterator implements Iterator {
128+
Node x = head;
129+
130+
@Override
131+
public boolean hasNext() {
132+
return x != null;
133+
}
134+
135+
@Override
136+
public Object next() {
137+
Object data = x.data;
138+
x = x.next;
139+
return data;
140+
}
141+
142+
}
143+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coding.basic;
2+
3+
public interface List {
4+
public void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
private LinkedList linkedList;
5+
6+
public Queue() {
7+
linkedList = new LinkedList();
8+
}
9+
10+
public void enQueue(Object o) {
11+
linkedList.add(o);
12+
}
13+
14+
public Object deQueue() {
15+
return linkedList.removeFirst();
16+
}
17+
18+
public boolean isEmpty() {
19+
return linkedList.size() == 0;
20+
}
21+
22+
public int size() {
23+
return linkedList.size();
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
5+
private LinkedList linkedList;
6+
7+
public Stack() {
8+
linkedList = new LinkedList();
9+
}
10+
11+
public void push(Object o) {
12+
linkedList.add(o);
13+
}
14+
15+
public Object pop() {
16+
return linkedList.removeLast();
17+
}
18+
19+
public Object peek() {
20+
return linkedList.get(linkedList.size()-1);
21+
}
22+
23+
public boolean isEmpty() {
24+
return linkedList.size() == 0;
25+
}
26+
27+
public int size() {
28+
return linkedList.size();
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.coding.basic;
2+
3+
public class Test {
4+
public static void main(String[] args) {
5+
LinkedList linkedList = new LinkedList();
6+
for (int i = 0; i < 10; i++) {
7+
linkedList.add(i);
8+
}
9+
for (int i = 0; i < 10; i++) {
10+
linkedList.removeLast();
11+
}
12+
linkedList.removeLast();
13+
}
14+
15+
}
Binary file not shown.

0 commit comments

Comments
 (0)