Skip to content

Commit

Permalink
Merge pull request #20 from eloiseSJTU/revert-19-revert-18-master
Browse files Browse the repository at this point in the history
第一次数据结构作业
  • Loading branch information
Rong Huang authored Feb 26, 2017
2 parents 97a370b + 5cfb6d5 commit e539934
Show file tree
Hide file tree
Showing 8 changed files with 670 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/18.
*/
public class MyArrayList<T> {

private Object[] initialArray = {};
private Object[] dataArray;
private int initSize = 10;
private int arraySize;
public MyArrayList() {
dataArray = initialArray;
}

public MyArrayList(int init) {
dataArray = new Object[init];
}

public void ensureCapacity(int newCapacity) {
if (newCapacity < arraySize)
return;

Object[] old = dataArray;
dataArray = new Object[newCapacity];
for (int i = 0; i < size(); i++) {
dataArray[i] = old[i];
}
}

public void add(T element) {
add(size(), element);
}

public void add(int index, T element) {
if (size() == dataArray.length) {
ensureCapacity(size()*2 + 1);
}
for(int i=arraySize;i>index;i--) {
dataArray[i] = dataArray[i - 1];
}
dataArray[index] = element;
arraySize++;
}

public T delete(int index) {
if (index < 0 || index > arraySize) {
throw new ArrayIndexOutOfBoundsException();
}
T removeElement = (T)dataArray[index];
for (int i = index; i < size() -1; i++) {
dataArray[i] = dataArray[i + 1];
}
arraySize--;
return removeElement;
}

public T get(int index) {
if (index < 0 || index > arraySize) {
throw new ArrayIndexOutOfBoundsException();
}
return (T)dataArray[index];
}

public T set(int index, T newElement) {
if (index < 0 || index > arraySize) {
throw new ArrayIndexOutOfBoundsException();
}
T oldElement = (T) dataArray[index];
dataArray[index] = newElement;

return oldElement;
}

public int size() {
return arraySize;
}

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

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

/**
* Created by andy on 2017/2/18.
*/
public class MyLinkedList<T> {
private class Node<T> {
public Node<T> pre;
public Node<T> next;
public T data;

public Node(Node<T> pre,Node<T> next,T data) {
this.pre = pre;
this.next = next;
this.data = data;
}
}

private int dataSize;

private Node head;
private Node tail;

public MyLinkedList() {
head = new Node<T>(null,null,null);
tail = new Node(head, null, null);
head.next = tail;
dataSize = 0;
}

public void add(T t) {
// add(size(), t);
Node<T> newNode = new Node<>(null, tail, t);
newNode.pre = tail.pre;
tail.pre.next = newNode;
tail.pre = newNode;
dataSize++;

}

/**
* 根据索引添加没有实现
* @param index
* @param element
*/
public void add(int index,T element) {
//TODO 根据索引添加元素
// addBefore(getNode(index,0,size()-1),element);
// if (index == dataSize) {
// add(element);
// } else {
//
// }
}

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

public T set(int index, T newValue) {
Node<T> node = getNode(index);
T oldData = node.data;
node.data = newValue;
return oldData;
}

public T remove(int index) {
Node<T> node = getNode(index);
node.next.pre = node.pre;
node.pre.next = node.next;
dataSize--;

return node.data;

}

private void addBefore(Node<T> node, T element) {
// newNode.pre.next = newNode;
// node.pre = newNode;
Node<T> pre = node.pre;
Node<T> newNode = new Node<>(node.pre, node, element);
node.pre = newNode;
pre.next = newNode;

dataSize++;
}

private Node<T> getNode(int index) {
return getNode(index, 0, size());
}

private Node<T> getNode(int index, int lower, int upper) {
Node<T> p;
if (index < lower || index > upper) {
throw new IndexOutOfBoundsException();
}

if (index < size() / 2) {
p = head.next;
for (int i = 0; i < index; i++) {
p = p.next;
}
} else {
p = tail.pre;
for (int i = size()-1; i > index; i--) {
p = p.pre;
}
}
return p;
}

public int size() {
return dataSize;
}

public boolean isEmpty() {
return size() == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/22.
*/
public class MyQueue<T> {
private MyLinkedList<T> link = new MyLinkedList<>();

public void enQueue(T t) {
link.add(t);
}

public T deQueue() {
if (size() <= 0) {
return null;
}
T t = link.get(0);
link.remove(0);
return t;
}

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

public int size() {
return link.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.lhpmatlab.coding2017.basic;

/**
* Created by andy on 2017/2/22.
*/
public class MyStack<T> {
private MyArrayList<T> list = new MyArrayList<>();

public void push(T t) {
list.add(t);
}

public T pop() {
if (size() <= 0) {
throw new IndexOutOfBoundsException();
}
return list.delete(size() - 1);
}

public T peek() {
return list.get(size() - 1);
}

public boolean isEmpty() {
return list.size() == 0;
}
public int size() {
return list.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.github.lhpmatlab.coding2017.basic;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class MyArrayListTest {
private MyArrayList<String> list;

@Before
public void init(){
list = new MyArrayList<>();
}

@Test
public void testEnsureCapacity() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.ensureCapacity(10);
assertEquals("ensureCapacity size is 10 ", list.size(),1);
}

/**
* 在列表的末尾添加元素
*/
@Test
public void testAddT() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.add("2");
assertEquals("add list size ", list.size(), 2);
for (int i=0; i<list.size(); i++) {
assertEquals("index of"+i,list.get(i),""+(i+1));
}
}

/**
* 测试在list的任意索引处添加元素
*/
@Test
public void testAddIntT() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.add("3");
list.add(1,"2");
assertEquals("add list size ", list.size(), 3);
assertEquals("add index 1 element is" ,list.get(1),"2");
for (int i=0; i<list.size(); i++) {
assertEquals("index of"+i,list.get(i),""+(i+1));
}
}

@Test
public void testDelete() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.add("3");
list.add(1,"2");
assertEquals("add list size ", list.size(), 3);
list.delete(1);
assertEquals("after delete index 1 ",list.get(1),"3");
}

@Test
public void testGet() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.add("2");
list.add("3");
for(int i=0; i<list.size();i++) {
assertEquals("index of"+i,list.get(i),""+(i+1));
}
}

@Test
public void testSet() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.add("3");
list.add("3");
list.set(1, "2");
for(int i=0; i<list.size();i++) {
assertEquals("index of"+i,list.get(i),""+(i+1));
}
}

@Test
public void testSize() {
assertEquals("init list size is 0 ", list.size(), 0);
list.add("1");
list.add("3");
list.add("3");
assertEquals("after add list size is ", list.size(),3);
}

/**
* 正确的判空用例
*/
@Test
public void testIsEmpty() {
assertEquals("list is empty", list.isEmpty(),true);
}

/**
* 失败的判空用例
*/
@Test
public void testIsEmptyNot() {
list.add("1");
assertEquals("list is empty", list.isEmpty(),false);
}
}
Loading

0 comments on commit e539934

Please sign in to comment.