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 #2 from eloiseSJTU/master
把组长的代码合过来
- Loading branch information
Showing
672 changed files
with
33,889 additions
and
44 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
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
101 changes: 101 additions & 0 deletions
101
group02/562768642/src/com/github/orajavac/coding2017/basic/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,101 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public class ArrayList implements List,Iterator{ | ||
|
||
private int size = 0; | ||
|
||
private int i=0; | ||
|
||
private Object[] elementData = new Object[2]; | ||
|
||
//SS | ||
public void add(Object o){ | ||
size=size(); | ||
if (size == elementData.length) | ||
grow(elementData,size); | ||
elementData[size]=o; | ||
} | ||
public void add(int index, Object o){ | ||
Object old=elementData[index]; //[3] old value=c | ||
size=size(); | ||
size+=1; | ||
if (size>elementData.length) | ||
grow(elementData,size); | ||
Object temp = null; | ||
int len=elementData.length; | ||
for (int i=0;i<len;i++){ | ||
if (i==index){ | ||
elementData[index]=o; | ||
}else if(i>index){ | ||
temp=elementData[i]; | ||
elementData[i]=old; //[4]=c | ||
old=temp; | ||
} | ||
} | ||
} | ||
|
||
public Object get(int index){ | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if (index==elementData.length-1){ //删除最后一个索引元素里的值 | ||
elementData[index]=null; | ||
}else{ | ||
elementData[index]=null; | ||
int len=elementData.length; | ||
for (int i=0;i<len;i++){ | ||
if (i==index){ | ||
elementData[i]=elementData[i+1]; | ||
}else if(i>index){ | ||
if(i+1!=len){ | ||
elementData[i]=elementData[i+1]; | ||
}else{ //我们假设数组索引 0-3,那么数组长度是4,3+1==4,elementData[i+1]会报错 | ||
elementData[i]=null; | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
size=0; | ||
for (int i=0;i<elementData.length;i++){ | ||
if (elementData[i]!=null){ | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
public void grow(Object[] elementData,int size){ | ||
Object[] target = new Object[size+elementData.length]; | ||
System.arraycopy(elementData, 0, target, 0, elementData.length); | ||
this.elementData=target; | ||
} | ||
|
||
public Iterator iterator(){ | ||
ArrayList l = new ArrayList(); | ||
l.elementData=this.elementData; | ||
l.size=this.size(); | ||
return l; | ||
} | ||
|
||
|
||
|
||
public boolean hasNext(){ | ||
if (i<=size-1){ | ||
return true; | ||
}else{ | ||
i=0; | ||
} | ||
return false; | ||
} | ||
|
||
public Object next(){ | ||
Object obj=elementData[i]; | ||
i++; | ||
return obj; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
group02/562768642/src/com/github/orajavac/coding2017/basic/BinaryTreeNode.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,32 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
return null; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group02/562768642/src/com/github/orajavac/coding2017/basic/Iterator.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,7 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
group02/562768642/src/com/github/orajavac/coding2017/basic/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,110 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public class LinkedList implements List,Iterator { | ||
|
||
private Node head; | ||
|
||
private Node current; | ||
|
||
private int size=0; | ||
|
||
public void add(Object o){ | ||
Node n = new Node(); | ||
n.next=this.head; | ||
n.data=o; | ||
this.head=n; | ||
} | ||
public void add(int index , Object o){ | ||
|
||
} | ||
public Object get(int index){ | ||
Node c = this.head; | ||
int i=0; | ||
while(c!=null){ | ||
i++; | ||
if (index==i){ | ||
return c.data; | ||
} | ||
c=c.next; | ||
} | ||
return null; | ||
} | ||
public Object remove(int index){ | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
size=0; | ||
Node c = this.head; | ||
while(c!=null){ | ||
size++; | ||
c=c.next; | ||
} | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
if (this.head==null){ | ||
this.head = new Node(); | ||
this.head.data=o; | ||
} | ||
} | ||
public void addLast(Object o){ | ||
add(o); | ||
} | ||
public Object removeFirst(){ | ||
int s=size(); | ||
int index=0; | ||
Node c = this.head; | ||
while(c!=null){ | ||
index=s--; | ||
if (index==2){ | ||
System.out.println(c.next.data); | ||
|
||
break; | ||
} | ||
c=c.next; | ||
} | ||
return null; | ||
} | ||
public Object removeLast(){ | ||
Node e = this.head.next; | ||
this.head=null; | ||
this.head=e; | ||
return null; | ||
} | ||
|
||
public void listNode(){ | ||
Node c = this.head; | ||
while(c!=null){ | ||
System.out.print(c.data+ " -> "); | ||
c=c.next; | ||
} | ||
System.out.println(); | ||
} | ||
public Iterator iterator(){ | ||
LinkedList l = new LinkedList(); | ||
l.head=this.head; | ||
return l; | ||
} | ||
|
||
public boolean hasNext(){ | ||
current = head; | ||
if (current!=null){ | ||
head = current.next; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public Object next(){ | ||
return current.data; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
group02/562768642/src/com/github/orajavac/coding2017/basic/List.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,9 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public interface List { | ||
public void add(Object o); | ||
public void add(int index, Object o); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
} |
34 changes: 34 additions & 0 deletions
34
group02/562768642/src/com/github/orajavac/coding2017/basic/Queue.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,34 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public class Queue { | ||
|
||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void enQueue(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
Object obj = elementData.get(0); | ||
elementData.remove(0); | ||
return obj; | ||
} | ||
|
||
public ArrayList getElementData() { | ||
return elementData; | ||
} | ||
|
||
public void setElementData(ArrayList elementData) { | ||
this.elementData = elementData; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if (elementData.size()>0) | ||
return true; | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
group02/562768642/src/com/github/orajavac/coding2017/basic/Stack.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,49 @@ | ||
package com.github.orajavac.coding2017.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
int s=elementData.size(); | ||
Object obj = null; | ||
for (int i=s-1;i>=0;i--){ | ||
if(elementData.get(i)!=null){ | ||
obj = elementData.get(i); | ||
elementData.remove(i); | ||
break; | ||
} | ||
} | ||
return obj; | ||
} | ||
|
||
public Object peek(){ | ||
int s=elementData.size(); | ||
Object obj = null; | ||
for (int i=s-1;i>=0;i--){ | ||
if(elementData.get(i)!=null){ | ||
obj = elementData.get(i); | ||
break; | ||
} | ||
} | ||
return obj; | ||
} | ||
public boolean isEmpty(){ | ||
if (elementData.size()>0) | ||
return true; | ||
return false; | ||
} | ||
public int size(){ | ||
return elementData.size(); | ||
} | ||
public ArrayList getElementData() { | ||
return elementData; | ||
} | ||
|
||
public void setElementData(ArrayList elementData) { | ||
this.elementData = elementData; | ||
} | ||
} |
Oops, something went wrong.