Skip to content

Commit 087a145

Browse files
authored
Merge pull request onlyliuxin#13 from PingPi357/master
HomeWork_1
2 parents 2466967 + b839c34 commit 087a145

File tree

9 files changed

+306
-0
lines changed

9 files changed

+306
-0
lines changed

group05/810574652/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

group05/810574652/.gitignore

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

group05/810574652/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>810574652HomeWork</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.github.PingPi357.coding2017.basic;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayList implements List {
6+
private static final int DEFAULT_CAPACITY = 100; // 定义静态final类型,
7+
// final在一个对象类唯一,
8+
// static
9+
// final在多个对象中都唯一,如果作为常量用的话,只是final的话就得在别的类引用的时候要创建对象,会占用不必要的空间,而加上static的话在编译的时候占用一个空间,其他时候就不会再占用空间了。所以常量一般用static修饰,其他时候看你自己的需要
10+
// 理解:比如我是一个维修工人....public相当于我接任何活,static相当于10分钟之内赶到,final相当于就我一家.
11+
// reference: http://bbs.itheima.com/thread-162971-1-1.html
12+
13+
int size = 0; // 如果是中文分号,报错:Syntax error on token "invalid Character", ;
14+
// excepted;
15+
16+
private Object[] elementData = new Object[DEFAULT_CAPACITY]; // new
17+
// Object后面接的是中括号
18+
19+
@Override
20+
public void add(Object o) {
21+
// TODO Auto-generated method stub
22+
ensureCapacity(++size); // size自加操作
23+
elementData[size - 1] = o;
24+
}
25+
26+
private void ensureCapacity(int minCapacity) {
27+
// TODO Auto-generated method stub
28+
if (minCapacity <= elementData.length) { // 当满足小于等于的情况 ???
29+
return;
30+
} else {
31+
elementData = Arrays.copyOf(elementData, minCapacity); // elementData写错为elementDta,导致报错 elementDta
32+
// cannot be
33+
// resolved
34+
// as a
35+
// variable
36+
// 这里为什么不直接是minCapacity 而要加上原始elementData.length????
37+
}
38+
}
39+
40+
@Override
41+
public void add(int index, Object o) {
42+
// TODO Auto-generated method stub
43+
if (index > size || index < 0) {
44+
throw new ArrayIndexOutOfBoundsException();
45+
}
46+
ensureCapacity(++size); // size自加操作
47+
for (int i = size - 2; i > index - 1; i--) { // for中用';'号隔开;
48+
// ???i应该从size-2开始赋值
49+
// ???终止条件为i>index-1
50+
elementData[i + 1] = elementData[i];
51+
}
52+
elementData[index] = o; // 插入元素
53+
}
54+
55+
@Override
56+
public Object remove(int index) {
57+
if (this.size > 0 && index < (this.size)) {
58+
Object o = elementData[index];
59+
for (int i = index; i < this.size; i++) {
60+
elementData[i] = elementData[i + 1];
61+
}
62+
this.size--;
63+
return o;
64+
} else {
65+
return null;
66+
}
67+
}
68+
69+
@Override
70+
public Object get(int index) {
71+
// TODO Auto-generated method stub
72+
return elementData[index]; // 必须进行角标越界检查吗???
73+
}
74+
75+
@Override
76+
public int size() {
77+
// TODO Auto-generated method stub
78+
return this.size;
79+
}
80+
81+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.github.PingPi357.coding2017.basic;
2+
3+
4+
//实现单向单端列表
5+
public class LinkedList implements List {
6+
private Node head = new Node(); // 定义链表的头节点
7+
private Node current;
8+
private int size;
9+
10+
public LinkedList() {
11+
head = null;
12+
size = 0;
13+
}
14+
15+
@Override
16+
public void add(Object o) {
17+
if (null == head) { // 头结点为空,直接放在头结点上
18+
head.data = o;
19+
head.next = null;
20+
size++;
21+
} else {
22+
current = head;
23+
while (!(current.next == null)) { // 找到链表的末尾节点,在其后增加
24+
current = current.next;
25+
}
26+
current.next = new Node();
27+
current.next.data = o;
28+
current.next.next = null;
29+
size++;
30+
}
31+
}
32+
33+
public void addFirst(Object o) {
34+
if (null == head) {
35+
head.data = o;
36+
head.next = null;
37+
size++;
38+
} else {
39+
current = new Node();
40+
current.data = o;
41+
current.next = head;
42+
head = current;
43+
size++;
44+
}
45+
}
46+
47+
public void addLast(Object o) {
48+
if (!(head == null)) {
49+
current = head;
50+
while (!(current.next == null)) {
51+
current = current.next;
52+
}
53+
current.next = new Node();
54+
current.next.data = o;
55+
current.next.next = current.next;
56+
size++;
57+
}
58+
}
59+
60+
@Override
61+
public void add(int index, Object o) {
62+
// TODO Auto-generated method stub
63+
64+
}
65+
66+
@Override
67+
public Object remove(int index) {
68+
// TODO Auto-generated method stub
69+
if (index < size && index > 1) { // 为什么index>1???
70+
current = head;
71+
for (int i = 0; i < index - 1; i++) {
72+
current = current.next; // 遍历到指定到index的上一个节点
73+
}
74+
current.next = current.next.next;
75+
size--;
76+
}
77+
return null;
78+
}
79+
80+
public Object removeFirst() {
81+
if (!(head == null)) {
82+
Node removeHead = head;
83+
head.next = head.next.next;
84+
size--;
85+
return removeHead;
86+
} else
87+
return null;
88+
}
89+
90+
public Object removeLast() {
91+
if (!(head == null)) {
92+
Node theNext = current.next;
93+
Node oldLast;
94+
while (theNext.next != null) {
95+
current = theNext;
96+
theNext = theNext.next;
97+
}
98+
oldLast = current.next;
99+
current.next = theNext.next;
100+
101+
size--;
102+
return oldLast;
103+
} else
104+
return null;
105+
106+
}
107+
108+
@Override
109+
public Object get(int index) {
110+
// TODO Auto-generated method stub
111+
if (index < size) {
112+
current = head;
113+
for (int i = 0; i < index; i++) {
114+
current = current.next;
115+
}
116+
return current.data;
117+
} else
118+
return null;
119+
}
120+
121+
@Override
122+
public int size() {
123+
return this.size;
124+
}
125+
126+
private static class Node { // 创建Node类,定义Node类的两个属性
127+
Object data;
128+
Node next;
129+
}
130+
131+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.github.PingPi357.coding2017.basic;
2+
3+
public interface List {
4+
public void add(Object o);
5+
6+
public void add(int index, Object o); // Object 首字母大写,不然会出现"object" can not
7+
// be resolved a type错误
8+
9+
public Object remove(int index);
10+
11+
public Object get(int index);
12+
13+
public int size(); // 获取List的实际大小,而length定义的是总的容量
14+
/*
15+
* public void change(int index, Object o); 实现改变特定index处的值
16+
*/
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.PingPi357.coding2017.basic;
2+
3+
public class Queue {
4+
private LinkedList linkedList = new LinkedList();
5+
6+
public void enQueue(Object o) {
7+
linkedList.addLast(o);
8+
}
9+
10+
public Object deQueue() {
11+
Object removeQueue = linkedList.removeFirst();
12+
return removeQueue;
13+
}
14+
15+
public int size() {
16+
return linkedList.size();
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.PingPi357.coding2017.basic;
2+
3+
import java.util.EmptyStackException;
4+
5+
public class Stack {
6+
private ArrayList elementData = new ArrayList();
7+
int size = 0;
8+
9+
public void push(Object o){
10+
elementData.add(o);
11+
size++;
12+
}
13+
14+
public Object pop(){
15+
if(this.isEmpty()==true)
16+
{
17+
throw new EmptyStackException();
18+
}
19+
ArrayList popData = (ArrayList) elementData.remove(elementData.size()-1);
20+
size--;
21+
return popData;
22+
}
23+
24+
public Object peek(){
25+
return elementData.get(elementData.size()-1);
26+
}
27+
public boolean isEmpty(){
28+
return elementData.size()==0;
29+
}
30+
public int size(){
31+
return this.size;
32+
}
33+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
代码实现主要参考了waking2017(441517454)和C-Bobo(183127807)两位的代码完成,
2+
自己仿照敲了一遍,加了些注释,同时在此基础上改了些

0 commit comments

Comments
 (0)