Skip to content

Commit

Permalink
Merge pull request #2 from dustheart/master
Browse files Browse the repository at this point in the history
基本数据结构作业
  • Loading branch information
844028312 authored Feb 24, 2017
2 parents 90a6fb5 + f5f5285 commit 8298a83
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 0 deletions.
97 changes: 97 additions & 0 deletions group04/1299310140/src/com/coding/basic/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.coding.basic;

public class ArrayList implements List {

private int size = 0;
private Object[] elementData = new Object[10];

public void add(Object o){
if(size < this.elementData.length){//加至末尾,size+1
this.elementData[size] = o;
size++;
}else{//扩张数组,然后加至末尾,size+1
this.elementData = grow(this.elementData,10);
this.elementData[size] = o;
size++;
}
}

public void add(int index, Object o){//-1<index<size+1 or 0=<index<=size
if(index < 0 || index > this.size){//index小于0or大于size,参数错误
return;
}
if(size >= this.elementData.length){//当前数组容量已满,需扩张
this.elementData = grow(this.elementData, 10);
}

//此时只需考虑将o加至index处(0=<index<=size && size<length)
if(index < size){//移动数据,然后插入o,size+1
for(int i = size;i > index;i--){
this.elementData[i] = this.elementData[i-1];
}
this.elementData[index] = o;
this.size++;
}else{//直接插入o,size+1
this.elementData[index] = o;
this.size++;
}

}

public Object get(int index){//-1<index<size or 0=<index<size
if(index < 0 || index >= this.size){//index小于0or大于等于size,参数错误
return null;
}
return this.elementData[index];
}

public Object remove(int index){//-1<index<size or 0=<index<size
if(index < 0 || index >= this.size){//index小于0or大于等于size,参数错误
return null;
}

Object o = this.elementData[index];//o保存被移除的值
//此时只需考虑将index处的o移除
for(int i = index;i < this.size-1;i++){
this.elementData[i] = this.elementData[i+1];
}
this.size--;
return o;
}

public int size(){
return this.size;
}

public Iterator iterator(){
return null;
}

/*
* 说明:扩张数组
* 参数:被扩张的原数组,扩张的增加数(扩张后数组的大小为原数组的长度+增加数)
* 返回值:扩张后的数组
*/
private static Object[] grow(Object[] src,int size){
// return Arrays.copyOf(src, src.length + size);
Object[] target = new Object[src.length + size];
System.arraycopy(src, 0, target, 0, src.length);
return target;
}

public String toString(){
String result = "[";
if(this.size == 0){
result = result + "]";
return result;
}else{
for(int i = 0;i < size;i++){
result = result + this.elementData[i] + ",";
}
result = result.substring(0,result.length()-1);
result = result + "]";
return result;
}
}

}
73 changes: 73 additions & 0 deletions group04/1299310140/src/com/coding/basic/BinaryTreeNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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){
if(this.data == null){//根节点为空
this.data = o;
}else{//根节点非空
BinaryTreeNode pres = this;
BinaryTreeNode insertNode = new BinaryTreeNode();
insertNode.setData(o);
while(pres != null){
if((int)o < (int)pres.data){//插入值<当前值,pres向左移动,或者将插入节点挂在当前节点左边
if(pres.left == null){
pres.left = insertNode;
break;
}
pres = pres.left;
}else{//插入值>=当前值,pres向右移动,或者将插入节点挂在当前节点右边
if(pres.right == null){
pres.right = insertNode;
break;
}
pres = pres.right;
}
}
}
return null;
}

public void print(){
if(this.data == null){
return;
}else{
if(this.left !=null){
this.left.print();
}
System.out.print(this.data);
System.out.print(" ");
if(this.right != null){
this.right.print();
}
}
}
}
183 changes: 183 additions & 0 deletions group04/1299310140/src/com/coding/basic/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package com.coding.basic;

public class LinkedList implements List {

private Node head;
private int size = 0;

public void add(Object o){
if(this.size == 0){//size为0,给head赋值
this.head = new Node(o);
size++;
}else{//将o加至链表末尾
Node Last = new Node(o);
Node pres = this.head;
while(pres.next != null){
pres = pres.next;
}
pres.next = Last;
size++;
}
}

public void add(int index , Object o){//index:0~size
if(index < 0 || index > this.size){//index小于0or大于size,参数错误
return;
}
if(index == 0){//将o加至链表头部
//size为0时,index必为0,执行该分支
Node first = new Node(o);
first.next = this.head;
this.head = first;
size++;
}else if(index == size){//将o加至链表末尾
//index == size != 0时,执行该分支
Node Last = new Node(o);
Node pres = this.head;
while(pres.next != null){
pres = pres.next;
}
pres.next = Last;
size++;
}else{
//0<index<size时,执行该分支
Node pres = this.head;//pres指向0
for(int i = 0;i < index-1;i++){
pres = pres.next;
}

//此时pres指向index-1
Node insert = new Node(o);
insert.next = pres.next;
pres.next = insert;
size++;
}
}

public Object get(int index){//index:0~size-1
if(index < 0 || index >= this.size){//index小于0or大于等于size,参数错误
return null;
}

Node pres = this.head;//pres指向0
for(int i = 0;i < index;i++){
pres = pres.next;
}
//此时pres指向index
return pres.data;
}

public Object remove(int index){//index:0~size-1
if(index < 0 || index >= this.size){//index小于0or大于等于size,参数错误
return null;
}

Object o = null;
if(index == 0){//删除头节点
o = this.head.data;
this.head = this.head.next;
size--;
}else{//删除头节点以外的其他节点
Node pres = this.head;//pres指向0
for(int i = 0;i < index-1;i++){
pres = pres.next;
}

//此时pres指向index-1
o = pres.next.data;
pres.next = pres.next.next;
size--;
}
return o;
}

public int size(){
return this.size;
}

public void addFirst(Object o){//同add(int 0 , Object o)
Node first = new Node(o);
first.next = this.head;
this.head = first;
size++;
}

public void addLast(Object o){//同add(int size , Object o)
if(this.size == 0){
this.head = new Node(o);
size++;
}else{//size>=1
Node Last = new Node(o);
Node pres = this.head;
while(pres.next != null){
pres = pres.next;
}
pres.next = Last;
size++;
}
}

public Object removeFirst(){//同remove(int 0)
if(this.size == 0){
return null;
}

Object o = this.head.data;
this.head = this.head.next;
size--;
return o;
}

public Object removeLast(){
if(this.size == 0){
return null;
}

Object o = null;
if(this.size == 1){//size==1
o = this.head.data;
this.head = null;
this.size--;
}else{//size>=2
Node pres = this.head;
while(pres.next.next != null){
pres = pres.next;
}
o = pres.next.data;
pres.next = null;
size--;
}
return o;
}

public Iterator iterator(){
return null;
}

private static class Node{
Object data;
Node next;

public Node(Object data) {
super();
this.data = data;
}
}

public String toString(){
String result = "[";
if(this.size == 0){
result = result + "]";
return result;
}else{
Node pres = this.head;
while(pres != null){
result = result + pres.data + ",";
pres = pres.next;
}
result = result.substring(0,result.length()-1);
result = result + "]";
return result;
}
}
}
29 changes: 29 additions & 0 deletions group04/1299310140/src/com/coding/basic/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.coding.basic;

public class Queue {
private LinkedList elementData = new LinkedList();

public void enQueue(Object o){
this.elementData.addLast(o);
}

public Object deQueue(){
return this.elementData.removeFirst();
}

public boolean isEmpty(){
if(this.elementData.size() == 0){
return true;
}else{
return false;
}
}

public int size(){
return this.elementData.size();
}

public String toString(){
return this.elementData.toString();
}
}
Loading

0 comments on commit 8298a83

Please sign in to comment.