Skip to content

Commit a0e7806

Browse files
authored
Merge pull request onlyliuxin#26 from tengjunjie1118/master
第一周大作业
2 parents 18ce0fe + 4ac3afd commit a0e7806

File tree

22 files changed

+873
-0
lines changed

22 files changed

+873
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>RemoteSystemsTempFiles</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
<nature>org.eclipse.rse.ui.remoteSystemsTempNature</nature>
11+
</natures>
12+
</projectDescription>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
26+
<classpathentry kind="output" path="target/classes"/>
27+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>dataStructure</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+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3+
org.eclipse.jdt.core.compiler.compliance=1.5
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.5
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>increaseLearning</groupId>
6+
<artifactId>dataStructure</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>dataStructure</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ListService;
2+
3+
//集合接口
4+
public interface KILinkedList<T> {
5+
6+
public void add(T t, int pos);
7+
8+
public T remove(int pos);
9+
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ListService;
2+
3+
//集合接口
4+
public interface KIList<T> {
5+
6+
public void add(T item);
7+
8+
public void add(int index, T item);
9+
10+
public void set(int index, T item);
11+
12+
public void remove(int index);
13+
14+
public void remove(T item);
15+
16+
public void clear();
17+
18+
public boolean contains(T item);
19+
20+
public boolean isEmpty();
21+
22+
public T get(int index);
23+
24+
public int indexOf(T item);
25+
26+
public int size();
27+
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ListService;
2+
3+
//集合接口
4+
public interface KIQueueList<T> {
5+
public T add(T ele);
6+
7+
public T remove();
8+
9+
public Object[] getData();
10+
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ListService;
2+
3+
//集合接口
4+
public interface KIStackList<T> {
5+
public void push(T ele);
6+
7+
public void pop();
8+
9+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package ListServiceImpl;
2+
3+
import ListService.KIList;
4+
5+
public class KArrayList<T> implements KIList<T> {
6+
7+
/** 初始化的容量的大小 */
8+
private final static int INIT_CAPACITY = 12;
9+
private Object[] mList = null;
10+
11+
/** 当前的容量 */
12+
private int mCurrentCapacity = 0;
13+
/** 容器中元素的个数 */
14+
private int mSize = 0;
15+
16+
public KArrayList() {
17+
mList = new Object[INIT_CAPACITY];
18+
mCurrentCapacity = INIT_CAPACITY;
19+
}
20+
21+
/**
22+
* 插入一个元素到链表尾部
23+
*
24+
* @param item
25+
*/
26+
public void add(T item) {
27+
if (mSize == mCurrentCapacity) {
28+
expansion();
29+
}
30+
mList[mSize] = item;
31+
mSize++;
32+
33+
}
34+
35+
/**
36+
* 插入一个元素到指定位置,从插入位置及其后面的元素往后移动一个位置
37+
*
38+
* @param index
39+
* 要插入的位置
40+
* @param item
41+
*/
42+
public void add(int index, T item) {
43+
if (index < 0 || index >= mSize) { // 不允许index小于0,或者index >= 数组当前大小
44+
throw new IndexOutOfBoundsException();
45+
}
46+
if (mSize == mCurrentCapacity) {
47+
expansion();
48+
}
49+
Object[] newList = new Object[mCurrentCapacity];
50+
System.arraycopy(mList, 0, newList, 0, index);
51+
System.arraycopy(mList, index, newList, index + 1, mSize - index);
52+
newList[index] = item;
53+
mList = newList;
54+
mSize++;
55+
56+
}
57+
58+
/**
59+
* 更新指定位置的元素
60+
*
61+
* @param index
62+
* @param item
63+
*/
64+
public void set(int index, T item) {
65+
if (index < 0 || index >= mSize) {
66+
throw new IndexOutOfBoundsException();
67+
}
68+
mList[index] = item;
69+
70+
}
71+
72+
/**
73+
* 移除指定位置的元素,后面的元素向前移动一位
74+
*
75+
* @param index
76+
*/
77+
public void remove(int index) {
78+
if (index < 0 || index >= mSize) {
79+
throw new IndexOutOfBoundsException();
80+
}
81+
Object[] newList = new Object[mCurrentCapacity];
82+
System.arraycopy(mList, 0, newList, 0, index);
83+
System.arraycopy(mList, index + 1, newList, index, mSize - index);
84+
mList = newList;
85+
mSize--;
86+
87+
}
88+
89+
/**
90+
* 移除链表中特定的元素。(如果item在链表中有多个,只移除第一个)
91+
*
92+
* @param item
93+
*/
94+
public void remove(T item) {
95+
for (int i = 0; i < mSize; i++) {
96+
if (mList[i].equals(item)) {
97+
remove(i);
98+
break;
99+
}
100+
}
101+
102+
}
103+
104+
/**
105+
* 将链表清空,capacity不变
106+
*/
107+
public void clear() {
108+
mList = new Object[mCurrentCapacity];
109+
mSize = 0;
110+
111+
}
112+
113+
/**
114+
* 判断是否包含某个元素
115+
*
116+
* @param item
117+
* @return true表示有这个元素,false表示没有这个元素
118+
*/
119+
public boolean contains(T item) {
120+
for (int i = 0; i < mSize; i++) {
121+
if (mList[i].equals(item)) {
122+
return true;
123+
}
124+
}
125+
return false;
126+
}
127+
128+
/**
129+
* 判断链表是否为空
130+
*
131+
* @return boolean
132+
*/
133+
public boolean isEmpty() {
134+
return (mSize == 0) ? true : false;
135+
}
136+
137+
/**
138+
* 获取指定位置的元素
139+
*
140+
* @param index
141+
* @return
142+
*/
143+
@SuppressWarnings("unchecked")
144+
public T get(int index) {
145+
if (index < 0 || index >= mSize) {
146+
throw new IndexOutOfBoundsException();
147+
}
148+
return (T) mList[index];
149+
}
150+
151+
/**
152+
* 获取特定元素所在的位置。 如果该链表中存在多个相同的元素,只返回第一个的位置,如果找不到,则返回-1。
153+
*
154+
* @param item
155+
* @return int 如果没找到,返回-1
156+
*/
157+
public int indexOf(T item) {
158+
for (int i = 0; i < mSize; i++) {
159+
if (mList[i].equals(item)) {
160+
return i;
161+
}
162+
}
163+
return -1;
164+
}
165+
166+
/**
167+
* 获取当前链表的长度
168+
*
169+
* @return int
170+
*/
171+
public int size() {
172+
return mSize;
173+
}
174+
175+
/**
176+
* 扩容,当 mSize == mCurrentCapacity 时调用
177+
*/
178+
private void expansion() {
179+
Object[] oldList = mList;
180+
Object[] newList = new Object[getNewCapacity()];
181+
System.arraycopy(oldList, 0, newList, 0, oldList.length);
182+
mList = newList;
183+
}
184+
185+
/**
186+
* 获取新的容量大小 当满的时候每次增加当前容量的50%
187+
*/
188+
private int getNewCapacity() {
189+
return mCurrentCapacity = mCurrentCapacity + (mCurrentCapacity >> 1);
190+
}
191+
192+
}

0 commit comments

Comments
 (0)