forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 15
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 #17 from lralin/master
the first homework
- Loading branch information
Showing
7 changed files
with
354 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
group20/872045674/20170226/src/com/coding/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,87 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[7]; | ||
|
||
public void add(Object o){ | ||
if(size>elementData.length-1){ | ||
ensureCapacity(size); | ||
} | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o){ | ||
System.out.println(elementData.length+" length"); | ||
System.out.println(size+" size"); | ||
size++; | ||
if(index<0||index>size||index>Integer.MAX_VALUE){ | ||
System.out.println("add 位置输入错误,请输入合理的位置"); | ||
return; | ||
} | ||
if(size>elementData.length-1){ | ||
ensureCapacity(size); | ||
} | ||
System.arraycopy(elementData,index,elementData,index+1,size-index-1); | ||
elementData[index] = o; | ||
} | ||
|
||
public Object get(int index){ | ||
if(index<0||index>size-1){ | ||
System.out.println("get 位置输入错误,请输入合理的位置"); | ||
return null; | ||
} | ||
|
||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index){ | ||
if(index<0||index>size-1){ | ||
System.out.println("remove 位置输入错误,请输入合理的位置"); | ||
return false; | ||
} | ||
System.arraycopy(elementData,index+1,elementData,index,size-index-1); | ||
elementData[--size]=null; | ||
return true; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
private void ensureCapacity(int nimCapacity){ | ||
int oldCapacity = elementData.length; | ||
int newCapacity = oldCapacity+(oldCapacity/2+1); | ||
if(newCapacity < nimCapacity){ | ||
newCapacity = nimCapacity; | ||
} | ||
if(newCapacity>Integer.MAX_VALUE){ | ||
newCapacity = Integer.MAX_VALUE; | ||
} | ||
elementData = Arrays.copyOf(elementData,newCapacity); | ||
} | ||
|
||
public static void main(String[] args) { | ||
ArrayList list=new ArrayList(); | ||
|
||
list.add(1); | ||
list.add(2); | ||
list.add(3); | ||
list.add(4); | ||
list.add(2,10); | ||
list.remove(3); | ||
for(int i=0;i<list.size();i++){ | ||
System.out.println(list.get(i)); | ||
} | ||
//System.out.println("temp"+list.toString()); | ||
|
||
//int x=2; | ||
//String[] l = {"a","d","v","cd","s"}; | ||
//System.out.println(l[x--]); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group20/872045674/20170226/src/com/coding/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.coding.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
group20/872045674/20170226/src/com/coding/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.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
152 changes: 152 additions & 0 deletions
152
group20/872045674/20170226/src/com/coding/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,152 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
|
||
public void add(Object o){ | ||
addFirst(o); | ||
} | ||
public void add(int index , Object o){ | ||
Node node = head; | ||
for(int i=0;i<index;i++){ | ||
node = node.next; | ||
} | ||
|
||
Node newNode=new Node(); | ||
newNode.data=o; | ||
newNode.next=node; | ||
add2Index(head,index,newNode); | ||
|
||
} | ||
private int count=0; | ||
private void add2Index(Node node,int index,Node nextNode){ | ||
count++; | ||
if(index==count){ | ||
node.next=nextNode; | ||
return; | ||
} | ||
add2Index(node.next,index,nextNode); | ||
} | ||
|
||
public Object get(int index){ | ||
Node node = head; | ||
for(int i=0;i<index;i++){ | ||
if(node.next==null){ | ||
System.out.println("位置输入错误"); | ||
break; | ||
} | ||
node = node.next; | ||
} | ||
return node.data; | ||
} | ||
public Object remove(int index){ | ||
return null; | ||
} | ||
|
||
public int size(){ | ||
int count=0; | ||
|
||
return count; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
public void addFirst(Object o){ | ||
Node newNode=new Node(); | ||
newNode.data=o; | ||
newNode.next=head; | ||
head=newNode; | ||
} | ||
public void addLast(Object o){ | ||
Node newNode=new Node(); | ||
newNode.data=o; | ||
if(head==null){ | ||
head=newNode; | ||
}else{ | ||
lastElement(head,o); | ||
} | ||
|
||
} | ||
|
||
private void lastElement(Node node,Object o){ | ||
if(node.next==null){ | ||
Node newNode=new Node(); | ||
newNode.data=o; | ||
node.next=newNode; | ||
return; | ||
} | ||
lastElement(node.next,o); | ||
} | ||
|
||
public Object removeFirst(){ | ||
if(head==null){ | ||
System.out.println("数据为空,不用删除"); | ||
return false; | ||
} | ||
head = head.next; | ||
return true; | ||
} | ||
public Object removeLast(){ | ||
removeLastNode(head); | ||
return true; | ||
} | ||
|
||
private void removeLastNode(Node node){ | ||
if(node.next==null){ | ||
node.data = null; | ||
return; | ||
} | ||
removeLastNode(node.next); | ||
} | ||
|
||
public Iterator iterator(){ | ||
return new LinkedListIterator(this); | ||
} | ||
private class LinkedListIterator implements Iterator{ | ||
private LinkedList list = null; | ||
private Node node=null; | ||
private LinkedListIterator(LinkedList l){ | ||
this.list = l; | ||
this.node = head; | ||
} | ||
@Override | ||
public boolean hasNext() { | ||
if(node.next==null){ | ||
return false; | ||
} | ||
node = node.next; | ||
return true; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
return node.data; | ||
} | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
} | ||
|
||
public static void main(String[] args) { | ||
LinkedList list = new LinkedList(); | ||
list.addFirst(1); | ||
list.addFirst(2); | ||
list.addFirst(3); | ||
list.addFirst(4); | ||
list.addLast(5); | ||
list.removeLast(); | ||
System.out.println(list.size()); | ||
//list.add(3,10); | ||
//System.out.println(list.get(4)); | ||
Iterator i=list.iterator(); | ||
while(i.hasNext()){ | ||
System.out.println(i.next()); | ||
} | ||
} | ||
} |
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.coding.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(); | ||
} |
44 changes: 44 additions & 0 deletions
44
group20/872045674/20170226/src/com/coding/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,44 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
private int size=0; | ||
|
||
private ArrayList list = new ArrayList(); | ||
|
||
public void enQueue(Object o){ | ||
list.add(o); | ||
size++; | ||
} | ||
|
||
public Object deQueue(){ | ||
if(size<=0){ | ||
System.out.println("队列为空"); | ||
return null; | ||
} | ||
size--; | ||
Object obj = list.get(0); | ||
list.remove(0); | ||
return obj; | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(size==0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Queue q=new Queue(); | ||
q.enQueue(1); | ||
q.enQueue(2); | ||
q.enQueue(3); | ||
q.enQueue(4); | ||
System.out.println(q.deQueue()); | ||
System.out.println(q.deQueue()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
group20/872045674/20170226/src/com/coding/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,23 @@ | ||
package com.coding.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o){ | ||
|
||
} | ||
|
||
public Object pop(){ | ||
return null; | ||
} | ||
|
||
public Object peek(){ | ||
return null; | ||
} | ||
public boolean isEmpty(){ | ||
return false; | ||
} | ||
public int size(){ | ||
return -1; | ||
} | ||
} |