Skip to content

Commit

Permalink
1259131938第一周作业
Browse files Browse the repository at this point in the history
汪耀进:第一周数据结构作业
  • Loading branch information
yichen10 committed Feb 26, 2017
1 parent d4a25d6 commit da509b4
Show file tree
Hide file tree
Showing 12 changed files with 325 additions and 0 deletions.
6 changes: 6 additions & 0 deletions group06/1259131938/JavaLearning/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions group06/1259131938/JavaLearning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions group06/1259131938/JavaLearning/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaLearning</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
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;
}

}
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();

}
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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.coding.basic;

/**
* @deprecated 用数组实现list
* @author wang
*
*/
public class MyArrayList implements List {

private int size = 0;

private Object[] elementData = new Object[100];

public void add(Object o){
// 确保数组大小
ensureCapacity(size + 1);
// 数组赋值并使得size+1;
elementData[size ++] = o;
size ++;
}

/**
* 确保数组不越界,否则就扩容;
* @param minCapacity list的大小;
*/
public void ensureCapacity(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = (oldCapacity / 3) * 2 + 1;
if (minCapacity > newCapacity) {
// 对数组扩容
Object[] newDate = new Object[newCapacity];
System.arraycopy(elementData, 0, newDate, 0, oldCapacity);
elementData = newDate;
}
}

public void add(int index, Object o){
// 对index进行校验:
rangeCheck(index);
ensureCapacity(size + 1);
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = o;
size ++;
}

/**
* 边界检查:
* @param index
*/
private void rangeCheck(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("size" + size + ", index:"
+ index);
}
}

public Object get(int index){
rangeCheck(index);
return elementData[index];
}

public Object remove(int index){
rangeCheck(index);
Object oldValue = elementData[index];
System.arraycopy(elementData, index, elementData, index - 1, size - index);
return oldValue;
}

public int size(){
return size;
}

public Iterator iterator(){
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.coding.basic;


public class MyLinkedList implements List {

private Node headNode;

private Node endNode;

private int size;

public void add(Object o){
add(size,o);
}
public void add(int index , Object o){
addBefore(getNode(index),o);
}

// 执行添加元素:
private void addBefore(Node node,Object o) {
Node newNode = new Node(o, node.prev, node.next);
newNode.prev.next = newNode;
newNode.next.prev = newNode;
size ++;
}

// 获得元素;
private Node getNode(int index) {
Node rtnNode;
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
}
if (index < size / 2) {
rtnNode = headNode.next;
for (int i = 0; i < index; i++) {
rtnNode = rtnNode.next;
}
} else {
rtnNode = endNode;
for (int i = size; i > index; i--) {
rtnNode = rtnNode.prev;
}
}
return rtnNode;
}

public Object get(int index){
return getNode(index).data;
}
public Object remove(int index){
return remove(getNode(index));
}

private Object remove(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
size --;
return node.data;
}
public int size(){
return size;
}

public void addFirst(Object o){
add(headNode.prev);
}
public void addLast(Object o){
add(endNode.next);
}
public Object removeFirst(){
remove(headNode);
return headNode.data;
}
public Object removeLast(){
remove(endNode);
return endNode.data;
}
public Iterator iterator(){
return null;
}

public boolean isEmpty(){
return size == 0;
}

private static class Node{
//当前元素,下一个及前一个;
Object data;
Node next;
Node prev;
public Node(Object data,Node prev, Node next) {
this.data = data;
this.next = next;
this.prev = prev;
}
}
}
36 changes: 36 additions & 0 deletions group06/1259131938/JavaLearning/src/com/coding/basic/MyStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.coding.basic;

public class MyStack {
private MyArrayList elementData = new MyArrayList();

/**
* 入栈
* @param o
*/
public void push(Object o){
elementData.add(o);
}

/**
* 出栈
* @return
*/
public Object pop(){
Object oldValue = elementData.get(elementData.size());
elementData.remove(elementData.size());
return oldValue;
}
/**
* 查看栈顶元素;
* @return
*/
public Object peek(){
return elementData.get(elementData.size());
}
public boolean isEmpty(){
return elementData.size() == 0;
}
public int size(){
return elementData.size();
}
}
20 changes: 20 additions & 0 deletions group06/1259131938/JavaLearning/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.coding.basic;

public class Queue {
MyLinkedList elementData = new MyLinkedList();
public void enQueue(Object o){
elementData.add(o);
}

public Object deQueue(){
return elementData.remove(elementData.size());
}

public boolean isEmpty(){
return elementData.size() == 0;
}

public int size(){
return elementData.size();
}
}
12 changes: 12 additions & 0 deletions group06/1259131938/RemoteSystemsTempFiles/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>RemoteSystemsTempFiles</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>org.eclipse.rse.ui.remoteSystemsTempNature</nature>
</natures>
</projectDescription>

0 comments on commit da509b4

Please sign in to comment.