Skip to content

Commit a61b9b9

Browse files
authored
Merge pull request onlyliuxin#2 from BaymaxGithub/master
Master
2 parents f3d1c22 + 0acb018 commit a61b9b9

File tree

10 files changed

+316
-0
lines changed

10 files changed

+316
-0
lines changed
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/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
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>20170224-01-ArrayList</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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package firstHomework.fan;
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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package firstHomework.fan;
2+
3+
public class myArrayList implements List {
4+
5+
private int size = 0;
6+
private int initLength=10;
7+
private Object[] elementData = new Object[initLength];
8+
9+
public void add(Object o){
10+
ensureCapacity(size+1);
11+
elementData[size++] = o;
12+
}
13+
14+
public void add(int index, Object o){
15+
ensureCapacity(size+1);
16+
if(index<0||index>size){
17+
System.out.println("index应该在0-size之间!!!");
18+
}else{
19+
System.arraycopy(elementData, index, elementData, index+1, size-index);
20+
elementData[index] = o;
21+
size++;
22+
}
23+
}
24+
25+
public Object get(int index){
26+
if(index<0||index>size){
27+
System.out.println("index应该在0-size之间!!!");
28+
}else{
29+
return elementData[index];
30+
}
31+
return null;
32+
}
33+
34+
public Object remove(int index){
35+
if(index<0||index>size){
36+
System.out.println("index应该在0-size之间!!!");
37+
return null;
38+
}
39+
Object obj = get(index);
40+
System.arraycopy(elementData, index+1, elementData, index, size-index-1);
41+
size--;
42+
return obj;
43+
}
44+
45+
public int size(){
46+
return this.size;
47+
}
48+
49+
50+
51+
public void ensureCapacity(int x){
52+
int oldCapacity = elementData.length;
53+
if(x>oldCapacity){
54+
Object newEle[] = new Object[oldCapacity+initLength];
55+
System.arraycopy(elementData, 0, newEle, 0, size);
56+
elementData = newEle;
57+
}
58+
}
59+
60+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package firstHomework.fan;
2+
3+
4+
5+
public class myLinkedList implements List {
6+
private static class Node{//链表的节点结构体
7+
Object data;//数据域
8+
Node next; //Node引用,相当于指针,指向下一个节点
9+
10+
Node(Object e, Node next) {
11+
this.data = e;
12+
this.next = next;
13+
}
14+
}
15+
16+
private Node head,last=null;//分别指向第一个和最后一个节点
17+
private int size;
18+
19+
20+
public void add(Object o){//在尾部添加,就相当于添加尾节点
21+
creatLastNode(o);
22+
}
23+
public void add(int index , Object o){//在index前面插入
24+
if(index == 0){//说明传过来的位置为0,那么就添加头结点
25+
createFirstNode(o);
26+
}else{//就去找到指定位置
27+
Node indexBeforeNode = getNode(index-1);//这里返回的是index的前一个节点
28+
Node newIndex =new Node(o,indexBeforeNode.next) ;//x新节点保存indexBefore的指向
29+
indexBeforeNode.next = newIndex;
30+
size++;
31+
}
32+
}
33+
public Object get(int index){
34+
return getNode(index).data;//返回的是节点中的数据对象
35+
}
36+
37+
public Object remove(int index){
38+
if(index==0){//移除头结点
39+
removeFirst();
40+
}else{//找到指定节点的前一个节点
41+
Node removeNode = getNode(index-1);
42+
removeNode.next = removeNode.next.next;//移除了index
43+
size--;
44+
return removeNode.next.data;//返回移除节点的数据对象
45+
}
46+
return null;
47+
}
48+
49+
public int size(){
50+
return this.size;
51+
}
52+
53+
public void addFirst(Object o){
54+
createFirstNode(o);
55+
}
56+
public void addLast(Object o){
57+
creatLastNode(o);
58+
}
59+
public Object removeFirst(){
60+
if(size>0){//列表不为空,即一定有头
61+
Node removeHead = head;
62+
head = head.next;
63+
size--;
64+
return removeHead.data;//返回头结点的数据对象
65+
}else{
66+
System.out.println("链表为空!");
67+
}
68+
return null;
69+
}
70+
public Object removeLast(){
71+
if(size>0){
72+
Node removeLastBefore = getNode(size-2);//找到last节点的上一个节点
73+
Object returnObj = removeLastBefore.next.data;
74+
removeLastBefore.next = null;
75+
last = removeLastBefore;
76+
size--;
77+
return returnObj;
78+
}else{
79+
System.out.println("链表为空!");
80+
}
81+
return null;
82+
}
83+
84+
/*
85+
* 添加头结点
86+
* */
87+
private void createFirstNode(Object e){
88+
Node oldHead = head;
89+
Node newHead = new Node(e,oldHead);//传进来的节点作为head节点的前一节点
90+
head = newHead;//不管空不空,head要指向新的头节点
91+
if(head == null){//如果链表为空,head和last都指向新节点(不为空last就不能乱赋值,因为不确定链表最后在哪)
92+
last = newHead;
93+
}else{//头结点已经存在,新节点作为头结点,原head变第二,last还是指向它的last
94+
newHead.next = head;
95+
}
96+
size++;
97+
}
98+
/*
99+
* 添加尾结点
100+
* */
101+
private void creatLastNode(Object e){
102+
Node oldLast = last;
103+
Node newLast = new Node(e,null);//新的尾节点下一个节点为空
104+
last = newLast;//不管空不空,last是要指向新的尾节点
105+
if(head == null){//链表为空
106+
head = newLast;
107+
}else{
108+
oldLast.next = newLast;
109+
}
110+
size++;
111+
}
112+
/*
113+
* 寻找指定结点
114+
* */
115+
private Node getNode(int index){
116+
if(index<0||index>=size){
117+
System.out.println("index越界!!!");
118+
}else{
119+
Node node=head;
120+
while(index != 0){
121+
node = node.next;
122+
index--;
123+
}
124+
return node;
125+
}
126+
return null;
127+
}
128+
129+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package firstHomework.fan;
2+
3+
public class myQueue {
4+
private int iniLength = 10;
5+
private Object[] array = new Object[iniLength];
6+
private int size = 0;
7+
8+
public void enQueue(Object o){
9+
if(size>=array.length){//需要扩容
10+
Object[] newArray = new Object[iniLength+array.length];//array数组原长再加上10
11+
System.arraycopy(array, 0, newArray, 0, size);
12+
array = newArray;
13+
}
14+
array[size++] = o;
15+
}
16+
17+
public Object deQueue(){//移除第一个元素,后面的元素整体向前位移
18+
Object deQueue = array[0];
19+
System.arraycopy(array, 1, array, 0, size-1);
20+
size--;
21+
return deQueue;
22+
23+
}
24+
25+
public boolean isEmpty(){
26+
return size==0;
27+
}
28+
29+
public int size(){
30+
return this.size;
31+
}
32+
public static void main(String[] args) {
33+
myQueue queue = new myQueue();
34+
queue.enQueue("A");
35+
queue.enQueue("B");
36+
queue.enQueue("C");
37+
38+
queue.enQueue("D");
39+
queue.enQueue("E");
40+
queue.enQueue("F");
41+
queue.enQueue("G");
42+
43+
while(!queue.isEmpty()){
44+
System.out.println(queue.deQueue());//出队
45+
}
46+
47+
}
48+
49+
50+
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package firstHomework.fan;
2+
3+
4+
5+
public class myStack {
6+
private myArrayList array = new myArrayList();//myArrayList对象里有个动态数组
7+
8+
public void push(Object o){ //入栈
9+
//长度不够时myArrayList会自己扩容
10+
array.add(o);//新对象放在数组后面
11+
}
12+
13+
public Object pop(){//出栈,即数组尾上的元素, 还要删除他
14+
Object pop = array.get(array.size()-1);//下标要比size小1
15+
array.remove(array.size()-1);
16+
return pop;
17+
}
18+
19+
public Object peek(){//只是弹出栈顶的值,不删除
20+
return array.get(array.size()-1);
21+
}
22+
public boolean isEmpty(){
23+
return array.size()==0;
24+
}
25+
public int size(){
26+
return array.size();
27+
}
28+
29+
30+
}
31+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
http://blog.csdn.net/stromcloud/article/details/56678442

0 commit comments

Comments
 (0)