forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from zhoubofeng/master
ArrayList,LinkedList,Queue,Stack
- Loading branch information
Showing
5 changed files
with
465 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
���� CPU���ڴ棬 Ӳ�̣�ָ��֮��Ĺ�ϵ | ||
|
||
http://blog.sina.com.cn/s/blog_986d02cd0102xncn.html | ||
|
||
QQ:598808350 |
186 changes: 186 additions & 0 deletions
186
group14/598808350/2017project/src/org/learning/container/ArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package org.learning.container; | ||
|
||
|
||
public class ArrayList { | ||
|
||
private Object [] objs = null; | ||
private int index = -1; | ||
public ArrayList(){ | ||
objs = new Object[5]; | ||
} | ||
public ArrayList(int size){ | ||
objs = new Object[size]; | ||
} | ||
/** | ||
* 返回一个新的数组 | ||
* @param src | ||
* @param src_index | ||
* @param dest | ||
* @param dest_index | ||
* @param length | ||
* @return | ||
*/ | ||
private static Object[] copy(Object[] src,int src_index,Object[] dest,int dest_index,int length){ | ||
System.arraycopy(src, src_index, dest, dest_index, length); | ||
return dest; | ||
} | ||
|
||
public void add(Object obj){ | ||
if(this.index == objs.length-1) { | ||
Object[] dest = new Object[objs.length+5]; | ||
objs = copy(objs,0,dest,0,objs.length); | ||
} | ||
this.index ++; | ||
objs[this.index] = obj; | ||
} | ||
|
||
public void add(int index,Object obj){ | ||
if(index-1 > this.index || index < 0){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object[] dest = new Object[objs.length+5]; | ||
if(index == 0){ | ||
dest[index] = obj; | ||
dest =copy(objs,index,dest,index+1,getSize()); | ||
objs = dest; | ||
}else if(index == getSize()){ | ||
objs[index] = obj; | ||
}else{ | ||
dest = copy(objs,0,dest,0,index);//前部分 | ||
dest[index] = obj; //中间部分 | ||
dest =copy(objs,index,dest,index+1,getSize()-index);//后部分 | ||
objs = dest; | ||
} | ||
this.index++; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index > this.index || index <0){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
return objs[index]; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(objs == null || this.index == -1){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int getSize(){ | ||
return this.index+1; | ||
} | ||
|
||
public boolean remove(int index){ | ||
if (index <0 || index > objs.length){ | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
Object[] dest = new Object[this.index]; | ||
dest = copy(objs,0,dest,0,index);//前部分 | ||
dest = copy(objs,index+1,dest,index,this.index-index);//后部分 | ||
objs = dest; | ||
this.index --; | ||
return true; | ||
} | ||
public boolean remove(Object obj){ | ||
for(int i=0;i<=this.index;i++){ | ||
if(obj==null ? get(i)==null : obj.equals(get(i))) { | ||
remove(i); //i 即 当前元素的下标识 | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
public static void print(Object obj){ | ||
System.out.println(obj); | ||
} | ||
|
||
public static void main(String [] args){ | ||
ArrayList al = new ArrayList(); | ||
/*print(al.isEmpty()); | ||
al.add("a1"); | ||
print(al.isEmpty()); | ||
print(al.getSize()); | ||
print(al.get(0)); | ||
print(al.get(1));*/ | ||
al.add("a0"); | ||
al.add("a1"); | ||
al.add("a2"); | ||
al.add("a3"); | ||
al.add("a4"); | ||
al.add("a5"); | ||
|
||
//al.remove(0); | ||
//al.remove(5); | ||
//al.remove(2); | ||
/*boolean flag = al.remove("a7"); | ||
print(flag); | ||
for(int i=0;i<al.getSize();i++){ | ||
print(al.get(i)); | ||
}*/ | ||
/*print(al.get(0)); | ||
print(al.get(5)); | ||
print(al.get(6));*/ | ||
//print(al.getSize()); | ||
//print(al.get(-1)); ok | ||
//print(al.get(0)); | ||
//print(al.get(5)); | ||
|
||
//print(al.get(6)); | ||
|
||
/*for(int i=0;i<al.getSize();i++){ | ||
print(al.get(i)); | ||
} | ||
print("---------------------以上内容为add");*/ | ||
|
||
al.add(0, "a00"); | ||
al.add(5, "a6"); | ||
for(int i=0;i<10;i++){ | ||
print(al.get(i)); | ||
} | ||
print("---------------------以上内容为add(index,obj)"); | ||
|
||
/*al.add(6, "a6"); | ||
al.add("a7"); | ||
for(int i=0;i<al.getSize();i++){ | ||
print(al.get(i)); | ||
} | ||
print("---------------------以上内容为add(index,obj)");*/ | ||
/*al.add("a0"); | ||
al.add("a1"); | ||
al.add("a2"); | ||
al.add("a3"); | ||
al.add("a4"); | ||
al.add("a5"); | ||
al.add("a6"); | ||
print(al.isEmpty()); | ||
print(al.getSize()); | ||
print(al.get(0)); | ||
print(al.get(5)); | ||
//al.add(5,"5.5"); | ||
for(int i=0;i<al.getSize();i++){ | ||
print("i"+i+":"+(al.get(i))); | ||
} | ||
//print(al.remove("a4")); | ||
print(al.getSize()); | ||
print(al.remove(2)); | ||
print("删除后"); | ||
print(al.getSize()); | ||
for(int i=0;i<al.getSize();i++){ | ||
print("i"+i+":"+(al.get(i))); | ||
} | ||
print("-----------add a7"); | ||
al.add("a7"); | ||
al.add(2,"a2");*/ | ||
|
||
/*for(int i=0;i<al.getSize();i++){ | ||
print("i"+i+":"+(al.get(i))); | ||
} | ||
print(al.get(6));*/ | ||
|
||
} | ||
|
||
} |
183 changes: 183 additions & 0 deletions
183
group14/598808350/2017project/src/org/learning/container/LinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package org.learning.container; | ||
|
||
public class LinkedList { | ||
|
||
private Node element = new Node(null,null,null); | ||
|
||
public LinkedList(){ | ||
this.element.next = (this.element.prev = this.element); | ||
} | ||
|
||
|
||
|
||
public Node getElement() { | ||
return element; | ||
} | ||
|
||
public void setElement(Node element) { | ||
this.element = element; | ||
} | ||
|
||
|
||
private int index = 0; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public void add(Object obj){ | ||
add(obj,this.element); | ||
} | ||
public Node add(Object obj,Node node){ | ||
// 将节点(节点数据是obj)添加到表头(element)之前 | ||
// 即,将节点添加到双向链表的末端 | ||
Node childNode = new Node(obj,node,node.prev); | ||
childNode.prev.next = childNode; | ||
childNode.next.prev = childNode; | ||
index += 1; | ||
return childNode; | ||
} | ||
|
||
/*public void add(int index,Object obj){ | ||
for(int i=0;i<size();i++){ | ||
if(i == index){ | ||
add(obj); | ||
} | ||
} | ||
}*/ | ||
|
||
public void addFirst(Object obj){ | ||
//add(0,obj); | ||
add(obj,this.element.next); | ||
} | ||
|
||
public Object getFirst(){ | ||
return element.next.obj; | ||
} | ||
|
||
public void addLast(Object obj){ | ||
//add(size(),obj); | ||
add(obj,this.element); | ||
} | ||
|
||
public Object getLast(){ | ||
return element.prev.obj; | ||
} | ||
public Node get(int index){ | ||
Node obj = null; | ||
for(int i=0;i<size();i++){ | ||
if(i == index) obj = element.next; | ||
} | ||
return obj; | ||
} | ||
public int size(){ | ||
return this.index ; | ||
} | ||
public Object remove(Node node){ | ||
Object e = node.obj; | ||
|
||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
|
||
node.prev = node.next = null; | ||
node.obj = null; | ||
index -=1; | ||
return e; | ||
} | ||
|
||
public void removeFirst(){ | ||
remove(this.element.next); | ||
} | ||
public void removeLast(){ | ||
remove(this.element.prev); | ||
} | ||
|
||
class Node{ | ||
Object obj ; //存放主体 | ||
Node prev ; //存放上一个对象 | ||
Node next ; //存放下一个对象。 | ||
public Node(Object obj,Node next,Node prev){ | ||
this.obj = obj; | ||
this.next = next; | ||
this.prev = prev; | ||
} | ||
} | ||
|
||
|
||
public static void main(String [] args){ | ||
/*Object [] objs = new Object[3]; | ||
Object [] c0 = new Object[2]; | ||
c0[0] = "c0"; | ||
Object [] c1 = new Object[2]; | ||
c1[0] = "c1"; | ||
Object [] c2 = new Object[2]; | ||
c2[0] = "c2"; | ||
c0[1] = c1; | ||
c1[1] = c2; | ||
c2[1] = null; | ||
objs[2] = c2; | ||
objs[1] = c1; | ||
objs[0] = c0; | ||
for(int i=0;i<objs.length;i++){ | ||
System.out.println("i="+i); | ||
System.out.println(((Object[])objs[i])[0]); | ||
if((Object[])((Object[])objs[i])[1] == null){ | ||
continue; | ||
} | ||
Object[] obj = (Object[])((Object[])objs[i])[1]; | ||
System.out.println(obj[0]); | ||
System.out.println(obj[1]); | ||
}*/ | ||
LinkedList list = new LinkedList(); | ||
list.add("test0"); | ||
list.add("test1"); | ||
list.add("test2"); | ||
|
||
list.addFirst("first"); | ||
list.addLast("last"); | ||
|
||
System.out.println(list); | ||
System.out.println("list.getFirst()"+list.getFirst()); | ||
System.out.println("list.getLast()"+list.getLast()); | ||
|
||
|
||
for(int i=0;i<list.size();i++){ | ||
Node tmp = list.get(i); | ||
print(tmp.obj); | ||
} | ||
|
||
System.out.println("----------------------------------------"); | ||
java.util.LinkedList linkedList = new java.util.LinkedList(); | ||
linkedList.add("object"); | ||
linkedList.addFirst("first"); | ||
//linkedList.addLast("last"); | ||
|
||
|
||
for(Object obj : linkedList){ | ||
print(obj); | ||
} | ||
|
||
print("--------------------添加后输出--------------------------------------"); | ||
|
||
print("-----------------删除first,last---------------------------------------"); | ||
|
||
linkedList.removeFirst(); | ||
linkedList.removeLast(); | ||
for(Object obj : linkedList){ | ||
print(obj); | ||
} | ||
|
||
|
||
} | ||
|
||
public static void print(Object obj){ | ||
System.out.println(""+obj); | ||
} | ||
} |
Oops, something went wrong.