Skip to content

Commit

Permalink
Merge pull request #6 from honokaBiu/master
Browse files Browse the repository at this point in the history
Sync
  • Loading branch information
conf1102 authored Mar 6, 2017
2 parents 358bed9 + 7436f62 commit bf7313a
Show file tree
Hide file tree
Showing 63 changed files with 2,697 additions and 252 deletions.
9 changes: 9 additions & 0 deletions group05/351592484/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/bin/
*.class
*.settings
*.project
*.classpath
*/.settings
*.iml
/.idea
/**/target/**/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.github.zhanglifeng.coding2017.basic;

import java.util.*;

/**
* 功能:实现ArrayList.
* @author zhanglifeng.
*/
public class ArrayList implements List {
private int size = 0; //当前数组大小

private Object[] elementData = new Object[5]; //初始数组

/**
* 将对象o添加到ArrayList中.
* @param o:需要添加的对象.
*/
public void add(Object o) {
ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容

elementData[size] = o; //将o添加到数组中
size++; //数组大小增加1
}

/**
* 将对象o添加到ArrayList的指定位置.
* @param index: 指定位置.
* @param o: 需要添加的对象.
*/
public void add(int index, Object o) {
rangeCheck(index); //判断指定的位置index是否合法

ensureCapacity(size + 1); //确保数组的容量可以装的下size + 1个元素,如果不够则扩容

System.arraycopy(elementData, index, elementData, index + 1, size - index); //将index位置到结束位置所有的数组往后移动一个位置
elementData[index] = o; //将对象o添加到index位置
size++;//数组大小增加1
}

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

public Object remove(int index) {
rangeCheck(index);

if (index != elementData.length - 1) {
System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index);
}

size--;
return elementData;
}

public int size() {
return size;
}

public Iterator iterator() {
return new ArrayListIterator(this);
}

private void ensureCapacity(int number) {
if (number > elementData.length) {
elementData = grow(elementData, 1);
}
}

public Object[] grow(Object[] src, int step) {
Object[] target = new Object[src.length + step];
System.arraycopy(src, 0, target, 0, src.length);
return target;
}

public void rangeCheck(int index){
if (index > size || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}

private class ArrayListIterator implements Iterator {
ArrayList arrayList = null;
int current = 0;

private ArrayListIterator(ArrayList arrayList) {
this.arrayList = arrayList;
}

@Override
public boolean hasNext() {
current++;
return current > arrayList.size() ? false : true;
}

@Override
public Object next() {
return elementData[current];
}

}

public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add("s1");
arrayList.add("s2");
arrayList.add("s3");
arrayList.add("s4");
arrayList.add(3, "s33");
arrayList.add("s5");

System.out.println(arrayList.size());

System.out.println(arrayList.get(2));

arrayList.remove(3);

System.out.println(arrayList.size());

arrayList.add("s1");
System.out.println(arrayList.size());
arrayList.remove(5);
System.out.println(arrayList.size());

Iterator it = arrayList.iterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}
System.out.println();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.zhanglifeng.coding2017.basic;

public class BinaryTreeNode {
private Object data;
private BinaryTreeNode left;
private BinaryTreeNode right;

public BinaryTreeNode(Object data) {
this.data = data;
left = null;
right = null;
}

public Object getData() {
return this.data;
}
public void setData(Object data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return this.left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return this.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.github.zhanglifeng.coding2017.basic;

public interface Iterator {
public boolean hasNext();
public Object next();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.github.zhanglifeng.coding2017.basic;

import java.util.NoSuchElementException;

/**
* 功能:实现LinkedList.
* @author zhanglifeng.
*/
public class LinkedList implements List {
private Node head, tail;
private int size;

private Node getNodeByIndex(int index) {
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("线性表索引越界");
}
Node current = head;
for (int i = 0; i < size && current != null; i++, current = current.next) {
if (i == index) {
return current;
}
}
return null;
}

public void add(Object o) {
addLast(o);
}

public void add(int index, Object o) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("线性表索引越界");
}

if (0 == index) {
addFirst(o);
}else{
Node node = getNodeByIndex(index - 1);
node.next = new Node(o, node.next);
size ++;
}
}

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

public Object remove(int index) {
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("线性表索引越界");
}

if(0 == index){
return removeFirst();
}else if(size - 1 == index){
return removeLast();
}else{
Node node = getNodeByIndex(index);
Node preNode = getNodeByIndex(index - 1);
preNode.next = node.next;
size --;
return node.data;
}
}

public int size() {
return size;
}

public void addFirst(Object o) {
Node currentHead = head;
Node newNode = new Node(o, currentHead);
head = newNode;
if(currentHead == null){
tail = newNode;
}

size++;
}

public void addLast(Object o) {
Node currentTail = tail;
Node newNode = new Node(o, null);
tail = newNode;
if(currentTail == null){
head = newNode;
}else {
currentTail.next = newNode;
}
size++;
}

public Object removeFirst() {
if(head == null){
throw new NoSuchElementException();
}
Node node = new Node(head.data, null);
head = head.next;
size --;
return node.data;
}

public Object removeLast() {
if(tail == null){
throw new NoSuchElementException();
}
Node node = getNodeByIndex(size - 1);
node.next = null;
size --;
return node.data;
}

public Iterator iterator() {
return new LinkedListIterator(this);
}

private static class Node {
Object data;
Node next;

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

private class LinkedListIterator implements Iterator {
LinkedList linkedList = null;
private int current = 0;

public LinkedListIterator(LinkedList linkedList) {
this.linkedList = linkedList;
}

@Override
public boolean hasNext() {
return current < size;
}

@Override
public Object next() {
return linkedList.get(current ++);
}
}

public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add("s1");
linkedList.add("s2");
linkedList.add("s3");
linkedList.addFirst("s0");
linkedList.addLast("s4");

Iterator it = linkedList.iterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}
System.out.println();
System.out.println("第3个元素:" + linkedList.get(3));

System.out.println(linkedList.removeFirst());
System.out.println(linkedList.size());
System.out.println("Last element:" + linkedList.removeLast());
System.out.println(linkedList.size());
System.out.println("第2个元素:" + linkedList.remove(2));
System.out.println(linkedList.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.zhanglifeng.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();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.zhanglifeng.coding2017.basic;

import java.util.EmptyStackException;

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

public Object deQueue(){
if (elementData.size() == 0) {
throw new EmptyStackException();
}
return elementData.removeFirst();
}

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

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

0 comments on commit bf7313a

Please sign in to comment.