Skip to content

Commit 6bb35fd

Browse files
committed
由于fork错了地址,重新提交第一次作业 2017-3-1 CoderXLoong
1 parent b72a63e commit 6bb35fd

File tree

93 files changed

+5353
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+5353
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package com.hans;
2+
3+
import java.util.Arrays;
4+
5+
import javax.management.RuntimeErrorException;
6+
7+
public class ArrayList implements List {
8+
9+
private int size = 0;//ArrayList中已经存储的元素个数,也是下一个存入的元素的下标。
10+
11+
private Object[] elementData = null;
12+
13+
public ArrayList() {
14+
elementData = new Object[3];
15+
}
16+
17+
public ArrayList(int capacity){
18+
elementData = new Object[capacity];
19+
}
20+
21+
// private static final final default
22+
23+
/**
24+
* 将参数o添加到ArrayList中最后一个元素的后面
25+
* @param o 添加的元素
26+
*/
27+
public void add(Object o){
28+
checkSize();
29+
// add(size++, o);
30+
elementData[size++] = o;
31+
}
32+
33+
/**
34+
* 将参数o添加到指定的index位置
35+
* index应满速 index >= 0 && index <= size() 为 true
36+
* @param index 添加的位置
37+
* @param o 添加的元素
38+
*/
39+
public void add(int index, Object o){
40+
if(index > size || index < 0){
41+
//size代表即将存入的元素的下标,所以index等于size也可以。
42+
throw new ArrayIndexOutOfBoundsException("Index:" + index);
43+
}
44+
checkSize();
45+
System.arraycopy(elementData, index, elementData, index + 1, size - index);
46+
elementData[index] = o;
47+
size++;
48+
}
49+
50+
/**
51+
* 获取输入的index位置处元素
52+
* index应满足 index >= 0 && index < size() 为 true
53+
*/
54+
public Object get(int index){
55+
checkIndex(index);
56+
return elementData[index];
57+
}
58+
59+
/**
60+
* 删除输入的index位置处的元素
61+
* index应满足 index >= 0 && index < size() 为 true
62+
* @return 被删除的元素
63+
*/
64+
public Object remove(int index){
65+
checkIndex(index);
66+
Object obj = elementData[index];
67+
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
68+
size--;
69+
return obj;
70+
}
71+
72+
/**
73+
* 获取当前ArrayList中已经存储的元素的个数
74+
*/
75+
public int size(){
76+
return this.size;
77+
}
78+
79+
/**
80+
* 获取当前ArrayList的迭代器
81+
* @return 当前ArrayList的迭代器
82+
*/
83+
public Iterator iterator(){
84+
return new Iterator(){
85+
private int count = 0;
86+
87+
@Override
88+
public boolean hasNext() {
89+
return size > count;
90+
}
91+
92+
@Override
93+
public Object next() {
94+
if(count == size){
95+
throw new RuntimeErrorException(null, "没有更多的元素!");
96+
}
97+
return get(count++);
98+
}
99+
100+
@Override
101+
public void remove() {
102+
ArrayList.this.remove(count - 1);
103+
}
104+
105+
};
106+
}
107+
108+
/**
109+
* 用来检查get和remove操作中输入的下标是否越界
110+
* @param index 输入的下标
111+
*/
112+
private void checkIndex(int index){
113+
if(index >= size || index < 0){
114+
//size代表即将存入的元素的下标,所以index下标上还没有存储内容,无法进行get和remove操作
115+
throw new ArrayIndexOutOfBoundsException("Index:" + index);
116+
}
117+
}
118+
119+
/**
120+
* 检查ArrayList是否将要存满。
121+
* 在ArrayList将要存满时对其进行扩容,每次扩容将使其容量增加20。
122+
*/
123+
private void checkSize(){
124+
if(size < elementData.length) return;
125+
elementData = Arrays.copyOf(elementData, elementData.length + 20/*(int)(elementData.length * 1.2)*/);
126+
}
127+
128+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.hans;
2+
3+
public class BinaryTree {
4+
BinaryTreeNode root = new BinaryTreeNode();
5+
6+
public boolean add(Object obj){
7+
8+
return true;
9+
}
10+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hans;
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.hans;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
public void remove();
7+
}
8+
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.hans;
2+
3+
public class LinkedList implements List {
4+
5+
private int size;
6+
7+
private Node head;
8+
9+
public LinkedList() {
10+
head = new Node();
11+
// head.data = head;
12+
}
13+
14+
/**
15+
* 添加一个元素
16+
*/
17+
public void add(Object o){
18+
Node tail = getTail();
19+
20+
Node node = new Node();
21+
node.data = o;
22+
23+
tail.next = node;
24+
size++;
25+
return;
26+
}
27+
28+
/**
29+
* 在指定位置加入一个元素
30+
* @param index 指定的位置,应该满足 index > 0 && index <= size() 为 true。
31+
*/
32+
public void add(int index , Object o){
33+
if(index < 0 || index > size)
34+
throw new IndexOutOfBoundsException("Index:" + index);
35+
36+
Node pos = head;
37+
for(int i = 0; i < index; i++){
38+
//要在index位置添加一个元素,只要获取到 index - 1 位置的元素即可
39+
pos = pos.next;
40+
}
41+
Node node = new Node();
42+
node.data = o;
43+
node.next = pos.next;
44+
pos.next = node;
45+
size++;
46+
return;
47+
}
48+
49+
/**
50+
* 获取指定位置处的元素
51+
* @param index 要获取的元素的位置,应该满足 index > 0 && index < size() 为 true。
52+
*/
53+
public Object get(int index){
54+
checkIndex(index);
55+
Node pos = head;
56+
for(int i = 0; i <= index; i++){
57+
//结束条件为 <= ,这是因为需要获取到index位置
58+
pos = pos.next;
59+
}
60+
return pos.data;
61+
}
62+
63+
/**
64+
* 移除并返回指定位置处的元素
65+
* @param index 要移除的元素的位置,应该满足 index > 0 && index < size() 为 true。
66+
*/
67+
public Object remove(int index){
68+
checkIndex(index);
69+
Node pos = head;
70+
for(int i = 0; i < index; i++){
71+
pos = pos.next;
72+
}
73+
Node temp = pos.next;
74+
pos.next = temp.next;
75+
size--;
76+
return temp.data;
77+
}
78+
79+
/**
80+
* 获取存储的元素的个数
81+
*/
82+
public int size(){
83+
return this.size;
84+
}
85+
86+
/**
87+
* 在第一个元素的前面出添加一个元素
88+
* @param o
89+
*/
90+
public void addFirst(Object o){
91+
add(0, o);
92+
}
93+
94+
/**
95+
* 在最后一个元素的后面添加一个元素
96+
* @param o
97+
*/
98+
public void addLast(Object o){
99+
add(o);
100+
}
101+
102+
/**
103+
* 移除第一个元素
104+
* @return 被移除的元素
105+
*/
106+
public Object removeFirst(){
107+
return remove(0);
108+
}
109+
110+
/**
111+
* 移除最后一个元素
112+
* @return 被移除的元素
113+
*/
114+
public Object removeLast(){
115+
return remove(size - 1);
116+
}
117+
118+
public Iterator iterator(){
119+
return null;
120+
}
121+
122+
123+
private static final class Node{
124+
Object data;
125+
Node next;
126+
}
127+
128+
/**
129+
* 获取最后一个节点
130+
* @return
131+
*/
132+
private Node getTail(){
133+
Node tail = head;
134+
while(tail.next != null){
135+
tail = tail.next;
136+
}
137+
return tail;
138+
}
139+
/**
140+
* 检查get和remove操作中所输入的元素是否有效
141+
* @param index
142+
*/
143+
private void checkIndex(int index) {
144+
if(index < 0 || index >= size)
145+
throw new IndexOutOfBoundsException("Index:" + index);
146+
}
147+
}
148+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.hans;
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+
public Iterator iterator();
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.hans;
2+
3+
public class Queue {
4+
5+
private LinkedList elementData = new LinkedList();
6+
7+
public void enQueue(Object o){
8+
elementData.add(o);
9+
}
10+
11+
public Object deQueue(){
12+
return elementData.removeFirst();
13+
}
14+
15+
public boolean isEmpty(){
16+
return elementData.size() == 0;
17+
}
18+
19+
public int size(){
20+
return elementData.size();
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hans;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
6+
public void push(Object o){
7+
elementData.add(o);
8+
}
9+
10+
public Object pop(){
11+
if(elementData.size() <= 0){
12+
return null;
13+
}
14+
return elementData.remove(elementData.size() - 1);
15+
}
16+
17+
public Object peek(){
18+
if(elementData.size() <= 0){
19+
return null;
20+
}
21+
return elementData.get(elementData.size() - 1);
22+
}
23+
public boolean isEmpty(){
24+
return elementData.size() == 0;
25+
}
26+
public int size(){
27+
return elementData.size();
28+
}
29+
}
30+

group13/1274639949/指令漫游记.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
如今计算机已经深入走进我们生活的方方面面,购物、娱乐、餐饮······,可以说我们无时无刻不在利用计算机给我们的

group13/1641296572/lesson1/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

0 commit comments

Comments
 (0)