Skip to content

Commit

Permalink
Merge pull request #33 from MrGPanPan/master
Browse files Browse the repository at this point in the history
Group 11 第一周作业提交
  • Loading branch information
onlyliuxin authored Feb 27, 2017
2 parents 99f53a7 + 452821d commit e0ab6d0
Show file tree
Hide file tree
Showing 88 changed files with 4,452 additions and 0 deletions.
10 changes: 10 additions & 0 deletions group11/1178243325/DataStructure/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'java'
apply plugin: 'eclipse'

jar {
manifest {
attributes 'Main-Class' : 'com.coding.Main'
}
}


1 change: 1 addition & 0 deletions group11/1178243325/DataStructure/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
实现基本的数据结构ArrayList,LinkList,Stack,Queue,Tree,Iterator
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.coding;

import com.coding.basic.*;
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList();

list.add(0, "2xxx");
list.add(1, "we");
list.add(2, "sss");
list.add("xing");
list.remove(2);
System.out.println(list.get(2));
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println(list.size());

LinkedList llist = new LinkedList();
llist.add("hu");
llist.add("zhao");
llist.add(2,"xing");
llist.addFirst("身骑白马");
llist.addLast("德州小老虎");
llist.add(5, "sf");
llist.remove(5);
llist.removeFirst();
llist.removeLast();
for (int i = 2; i >=0; i--)
System.out.print(llist.get(i));
System.out.println(llist.size());

Iterator literator = llist.iterator();
while(literator.hasNext()) {
System.out.println(literator.next());
}

Stack stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println(stack.peek());
while(!stack.isEmpty())
System.out.println(stack.pop());

Queue queue = new Queue();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
System.out.println(queue.size());
while (!queue.isEmpty()) {
System.out.println(queue.deQueue());
}

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

import com.coding.basic.exception.*;
public class ArrayList implements List {

private int size;
private Object[] elementData;

public ArrayList () {
size = 0;
elementData = new Object[100];
}

public void add(Object o){
add(size(), o);
}

public void add(int index, Object o){
if (size() == elementData.length)
ensureCapacity( size() * 2 + 1);
if (index > size() || index < 0) { //index == size时相当于在尾后插入
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
for (int i = size; i > index; i--) {
elementData[i] = elementData[i-1];
}
elementData[index] = o;
size++;

}

private void ensureCapacity(int newCapacity) {
if (newCapacity < size())
return;
Object[] old = elementData;
elementData = new Object[newCapacity];
for (int i = 0; i < size(); i++) {
elementData[i] = old[i];
}
}

public Object get(int index){
if (index >= size() || index < 0) { //获取时,index==size()越界
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
return elementData[index];
}

private String outOfBoundsMsg(int index) {
return "Index:" + index + ", Size:" + size;
}

public Object remove(int index){
if (index >= size() || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
Object old = elementData[index];
for (int i = index; i < size(); i++) {
elementData[i] = elementData[i+1];
}
size--;
return old;
}

/*获取表内容量*/
public int size(){
return size;
}

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

public class ArrayListIterator implements Iterator {
private final int ONLY_CAPACITY = size;
private int index;
public ArrayListIterator() {
index = 0;
}

@Override
public boolean hasNext() {
if (ONLY_CAPACITY != size)
throw new ConcurrentModificationException("此对象没有进行修改同步");
return index != size;
}

@Override
public Object next() {
if (ONLY_CAPACITY != size)
throw new ConcurrentModificationException("此对象没有进行修改同步");
if (index >= ONLY_CAPACITY)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
return elementData[index++];
}
}
}
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,6 @@
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,135 @@
package com.coding.basic;

import com.coding.basic.exception.*;
public class LinkedList implements List {

private Node head;
private int size;
public LinkedList() {
head = new Node(null, null);
size = 0;
}

public void add(Object o){
add(size, o);
}

public void add(int index , Object o){
if (index > size || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
Node frontNode = getNode(index-1);
Node newNode = new Node(o, frontNode.next);
frontNode.next = newNode;
size++;

}

private Node getNode(int index) {
Node node = head;
int i = 0;
while(node.next != null && i <= index) {
node = node.next;
i++;
}
return node;
}

public Object get(int index){
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

Node node = getNode(index);
return node.data;
}

public Object remove(int index){
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
Node frontNode = getNode(index-1);
Node oldNode = getNode(index);
frontNode.next = oldNode.next;
size--;
return oldNode.data;
}

public int size(){
return size;
}

public void addFirst(Object o){
//就是这么硬!
add(0, o);
}

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

public Object removeFirst(){
return remove(0);
}

public Object removeLast(){
return remove(size-1);
}

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

private class LinkedListIterator implements Iterator {
int index;
final int capacity = size;
LinkedListIterator() {
index = 0;
}
@Override
public boolean hasNext() {
if (capacity != size)
throw new ConcurrentModificationException("此对象没有修改同步");
return index < capacity;
}

@Override
public Object next() {
if (capacity != size)
throw new ConcurrentModificationException("此对象没有修改同步");
if (index >= capacity)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
return get(index++);
}
}

private String outOfBoundsMsg(int index) {
return "index:" + index + ", size:" + size;
}

private static class Node {
Object data;
Node next;

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

void setData(Object data) {
this.data = data;
}

Object getData() {
return data;
}

void setNext(Node next) {
this.next = next;
}

Object getNext() {
return 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,30 @@
package com.coding.basic;
import com.coding.basic.exception.*;
public class Queue {

private LinkedList elementData;

public Queue() {
elementData = new LinkedList();
}

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

public Object deQueue(){
if (isEmpty()) {
throw new EmptyQueueException("队空");
}
Object result = elementData.removeFirst();
return result;
}

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

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

0 comments on commit e0ab6d0

Please sign in to comment.