forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 11
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 #18 from a320321wb/master
homework1
- Loading branch information
Showing
8 changed files
with
426 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
group12/2319021847/homework1/com/code/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,106 @@ | ||
package com.coding.basic; | ||
|
||
public class ArrayList implements List{ | ||
|
||
private int size = 0; | ||
private Object[] elements; | ||
|
||
public ArrayList() { | ||
this.elements = new Object[5]; | ||
} | ||
|
||
public ArrayList(int size) { | ||
this.elements = new Object[size]; | ||
} | ||
public void add(Object o) { | ||
// TODO Auto-generated method stub | ||
if(isFull()) | ||
{ | ||
resize(); | ||
} | ||
this.elements[this.size] = o; | ||
this.size++; | ||
} | ||
public boolean isFull () | ||
{ | ||
if(this.size == this.elements.length) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void resize() | ||
{ | ||
Object[] newElements = new Object[this.elements.length*2]; | ||
System.arraycopy(elements, 0, newElements, 0, elements.length); | ||
this.elements = newElements; | ||
newElements = null; | ||
} | ||
public void add(int index, Object o) { | ||
// TODO Auto-generated method stub | ||
rangeCheck(index); | ||
if(isFull()) | ||
{ | ||
resize(); | ||
} | ||
System.arraycopy(elements, index, elements, index+1,size-index); | ||
this.elements[index] = o; | ||
size++; | ||
} | ||
void rangeCheck(int index) | ||
{ | ||
if(index > size || index < 0) | ||
{ | ||
throw new IndexOutOfBoundsException("ϱêÔ½½ç"); | ||
} | ||
} | ||
public Object get(int index) { | ||
// TODO Auto-generated method stub | ||
rangeCheck(index); | ||
return elements[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
// TODO Auto-generated method stub | ||
rangeCheck(index); | ||
Object elem = elements[index]; | ||
System.arraycopy(elements, index+1, elements, index, size-index-1); | ||
size--; | ||
elements[size] = null; | ||
return elem; | ||
} | ||
|
||
public int size() { | ||
// TODO Auto-generated method stub | ||
return this.size; | ||
} | ||
public com.coding.basic.Iterator Iterator () | ||
{ | ||
return new Itr(); | ||
} | ||
public class Itr implements com.coding.basic.Iterator{ | ||
int cur = 0; | ||
public boolean hasNext() { | ||
// TODO Auto-generated method stub | ||
if(size==cur) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public Object next() { | ||
// TODO Auto-generated method stub | ||
int i = cur; | ||
if(i < elements.length) | ||
{ | ||
cur = i+1; | ||
return elements[i]; | ||
} | ||
return null; | ||
} | ||
|
||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
group12/2319021847/homework1/com/code/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,53 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
private BinaryTreeNode root; | ||
|
||
public BinaryTreeNode(Object o){ | ||
this.data = o; | ||
this.left = null; | ||
this.right = null; | ||
} | ||
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(BinaryTreeNode root, Object o){ | ||
BinaryTreeNode current = root; | ||
BinaryTreeNode node = new BinaryTreeNode(o); | ||
if(current == null) | ||
{ | ||
current = node; | ||
} | ||
else if(((Integer)current.getData()).intValue() < ((Integer)o).intValue()) | ||
{ | ||
insert(current.getLeft(),o); | ||
} | ||
else if(((Integer)current.getData()).intValue() >= ((Integer)o).intValue()) | ||
{ | ||
insert(current.getRight(),o); | ||
} | ||
|
||
return node; | ||
} | ||
|
||
} |
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,6 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
boolean hasNext(); | ||
Object 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,86 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkList implements List{ | ||
|
||
private LinkNode head; | ||
// TODO Auto-generated method stub | ||
private int size; | ||
|
||
public LinkList() | ||
{ | ||
this.head = null; | ||
this.size = 0; | ||
} | ||
public void add(Object o) { | ||
// TODO Auto-generated method stub | ||
LinkNode currPtr = this.head; | ||
if(this.head == null) | ||
{ | ||
this.head = new LinkNode(o, this.head ,null); | ||
} | ||
while(currPtr != null) | ||
{ | ||
currPtr = currPtr.getNext(); | ||
} | ||
currPtr.setNext(new LinkNode(o, currPtr, null)); | ||
size++; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
// TODO Auto-generated method stub | ||
LinkNode currPtr = this.head; | ||
if(index < 0 || index > size) | ||
throw new IndexOutOfBoundsException("ϱêÔ½½ç"); | ||
int i = 0; | ||
if(index == 0) | ||
{ | ||
LinkNode node = new LinkNode(o,currPtr,currPtr.getPrv()); | ||
currPtr.getNext().setPrv(node); | ||
currPtr = node; | ||
} | ||
while(i < index) | ||
{ | ||
currPtr = currPtr.getNext(); | ||
i++; | ||
} | ||
LinkNode node = new LinkNode(o,currPtr.getPrv(),currPtr); | ||
currPtr.getPrv().setNext(node); | ||
currPtr.setPrv(node); | ||
size++; | ||
} | ||
|
||
public Object get(int index) { | ||
if(index < 0 || index > size) | ||
throw new IndexOutOfBoundsException("ϱêÔ½½ç"); | ||
int i = 0; | ||
LinkNode currPtr = this.head; | ||
while(i < index) | ||
{ | ||
currPtr = currPtr.getNext(); | ||
i++; | ||
} | ||
|
||
return currPtr.getData(); | ||
} | ||
|
||
public Object remove(int index) { | ||
// TODO Auto-generated method stub | ||
int i = 0; | ||
LinkNode currPtr = this.head; | ||
while(i < index) | ||
{ | ||
currPtr = currPtr.getNext(); | ||
i++; | ||
} | ||
currPtr.getNext().setPrv(currPtr.getPrv()); | ||
currPtr.getPrv().setNext( currPtr.getNext()); | ||
size--; | ||
return currPtr.getData(); | ||
} | ||
|
||
public int size() { | ||
// TODO Auto-generated method stub | ||
return size; | ||
} | ||
|
||
} |
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,46 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkNode { | ||
private LinkNode pNextPtr; | ||
private LinkNode pPrvPtr; | ||
private Object ndata; | ||
|
||
public LinkNode () | ||
{ | ||
this.pNextPtr=null; | ||
this.pPrvPtr=null; | ||
this.ndata=null; | ||
} | ||
|
||
public LinkNode (Object o, LinkNode pPrvPtr, LinkNode pNextPtr) | ||
{ | ||
this.pNextPtr=null; | ||
this.pPrvPtr=null; | ||
this.ndata=null; | ||
} | ||
|
||
public void setNext(LinkNode node) | ||
{ | ||
this.pNextPtr = node; | ||
} | ||
|
||
public void setPrv(LinkNode node) | ||
{ | ||
this.pPrvPtr = node; | ||
} | ||
|
||
public LinkNode getNext() | ||
{ | ||
return this.pNextPtr; | ||
} | ||
|
||
public LinkNode getPrv() | ||
{ | ||
return this.pPrvPtr; | ||
} | ||
public Object getData() | ||
{ | ||
return this.ndata; | ||
} | ||
|
||
} |
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,14 @@ | ||
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(); | ||
|
||
} |
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,70 @@ | ||
package com.coding.basic; | ||
|
||
public class Queue { | ||
private int size; | ||
|
||
private int head; | ||
|
||
private int tail; | ||
|
||
private Object[] queue; | ||
|
||
public Queue() | ||
{ | ||
this.size = 0; | ||
this.head = 0; | ||
this.tail = 0; | ||
} | ||
|
||
public void enQueue(Object o) { | ||
if(isFull()) | ||
{ | ||
resize(); | ||
} | ||
int newtail = (head+size)%queue.length; | ||
queue[newtail] = o; | ||
size++; | ||
} | ||
|
||
public Object deQueue() { | ||
if(isEmpty()) | ||
{ | ||
return null; | ||
} | ||
Object oldHead = queue[head]; | ||
head = (head-1)%queue.length; | ||
size--; | ||
return oldHead; | ||
} | ||
|
||
public int getHead(){ | ||
return head; | ||
} | ||
|
||
public int getTail(){ | ||
return tail; | ||
} | ||
|
||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public int size() { | ||
int diff = tail - head; | ||
return diff; | ||
} | ||
|
||
// | ||
public boolean isFull(){ | ||
return size == queue.length; | ||
} | ||
|
||
|
||
public void resize(){ | ||
Object[] newQueue = new Object[2*(queue.length)]; | ||
System.arraycopy(queue, 0, newQueue, 0, size); | ||
this.queue = newQueue; | ||
newQueue = null; | ||
} | ||
} |
Oops, something went wrong.