Skip to content

Commit

Permalink
Merge pull request #15 from wys2017/master
Browse files Browse the repository at this point in the history
第一次作业
  • Loading branch information
wizardzhang2017 authored Mar 13, 2017
2 parents 030ea7f + 4a32c39 commit c9fcb66
Show file tree
Hide file tree
Showing 13 changed files with 608 additions and 0 deletions.
7 changes: 7 additions & 0 deletions group27/2471486765/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 6 additions & 0 deletions group27/2471486765/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/bin/

/.setting/
/.classpath
/.project

17 changes: 17 additions & 0 deletions group27/2471486765/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>2471486765Learning</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>
110 changes: 110 additions & 0 deletions group27/2471486765/src/com/mycoding/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.mycoding;

import java.util.NoSuchElementException;

public class ArrayList implements List {

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


//增加元素
public void add(Object o) {
if (size()>=elementData.length) {
elementData = ArrayList.grow(elementData,elementData.length);
}
elementData[size++] = o;
}

//在指定位置上增加元素
public void add(int index, Object o) {
if(index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
else if(index == 0 || index > 0 && index < size()) {
elementData = ArrayList.grow(elementData,elementData.length);
for(int i=size()-1;i>=index;i--) {
elementData[i+1] = elementData[i];
}
elementData[index] = o;
size++;
}
else if(index == size()) {
elementData = ArrayList.grow(elementData,elementData.length);
elementData[size++] = o;
}
}

//返回此列表中指定位置上的元素
public Object get(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException();
} else {
return elementData[index];
}
}

//删除指定位置的元素
public Object remove(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException();
}
if(index == 0 || index > 0 && index < size()) {
for(int i=index;i<size()-1;i++){
elementData[i] = elementData[i+1];
}
size--;
}
return elementData;
}

public int size() {
return size;
}

//扩容
public static Object[] grow(Object[] src,int size) {
Object[] target = new Object[2*size];
System.arraycopy(src, 0, target, 0, size);
return target;
}

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

}

private class ArrayListIterator implements Iterator {
@SuppressWarnings("unused")
ArrayList array = null;
int cursor=0;
private ArrayListIterator(ArrayList array) {
this.array = array;
}

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

@Override
public Object next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
return elementData[cursor++];
}
}

@Override
public String toString() {
StringBuffer str = new StringBuffer();
str.append("[");
for (int i = 0; i < size(); i++) {
str.append(elementData[i]+",");
}
str.deleteCharAt(str.length()-1);
str.append("]");
return str.toString();
}
}
7 changes: 7 additions & 0 deletions group27/2471486765/src/com/mycoding/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mycoding;

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

}
170 changes: 170 additions & 0 deletions group27/2471486765/src/com/mycoding/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.mycoding;

import java.util.NoSuchElementException;

public class LinkedList implements List {
private Node head;
private int size;


public LinkedList() {
this.head = null;
this.size = 0;
}
//添加新元素
public void add(Object o){
Node newNode = new Node(o);
if(head == null) {
head = newNode;
} else {
Node nextNode = head;
while(nextNode.next != null) {
nextNode = nextNode.next;
}
nextNode.next = new Node(o);
}
size++;
}

//在指定位置添加新元素
public void add(int index , Object o){
checkAddElementIndex(index);
Node addNode = head;
if(index == 0) {
addFirst(o);
} else {
for(int i=0;i<index-1;i++) {
addNode = addNode.next;
}
Node insertNode = new Node(o);
insertNode.next = addNode.next;
addNode.next = insertNode;
size++;
}
}

public Object get(int index){
checkGetElementIndex(index);
Node getNode = head;
for(int i=0;i<index;i++) {
getNode = getNode.next;
}
return getNode.data;
}

public Object remove(int index){
checkGetElementIndex(index);
Node indexNode = head;

if(index == 0) {
Node laterNode = head;
while (indexNode.next != null) {
laterNode.data = indexNode.next.data;
laterNode = laterNode.next;
indexNode = indexNode.next;
}
size--;
return laterNode.data;
} else {
if(index > 0 && index < size()) {
for (int i=0;i<index-1;i++) {
indexNode = indexNode.next;
}
indexNode.next = indexNode.next.next;
size--;
}
return indexNode.data;
}
}


public int size(){
return size;
}

public void addFirst(Object o){
Node addFirstNode = new Node(o);
addFirstNode.next = head;
head = addFirstNode;
size++;
}


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


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

public Iterator iterator(){
return new LinkedListIterator(this);
}
private class LinkedListIterator implements Iterator {
@SuppressWarnings("unused")
LinkedList linked = null;
int cursor=0;
private LinkedListIterator(LinkedList linked) {
this.linked = linked;
}

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

@Override
public Object next() {
if(!hasNext()) {
throw new NoSuchElementException();
}
return get(cursor++);
}
}

public void checkGetElementIndex(int index) {
if (index < 0 || index >= size()){
throw new IndexOutOfBoundsException();
}
}

public void checkAddElementIndex(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException();
}
}
public void isEmpty() {
if(head == null) {
throw new NoSuchElementException();
}
}

private static class Node{
Object data;
Node next;

public Node(Object o){
data = o;
next = null;
}
}


@Override
public String toString() {
StringBuffer str = new StringBuffer();
str.append("[");
for(int i=0; i<size; i++){
str.append( this.get(i) + ",");
}
str.deleteCharAt(str.length()-1);
str.append("]");
return str.toString();
}
}
11 changes: 11 additions & 0 deletions group27/2471486765/src/com/mycoding/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mycoding;

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();
public Iterator iterator();
}
37 changes: 37 additions & 0 deletions group27/2471486765/src/com/mycoding/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mycoding;

import java.util.NoSuchElementException;

public class Queue {

private LinkedList linked = new LinkedList();

public void enQueue(Object o) {
linked.add(o);
}

public Object deQueue() {

if(isEmpty()) {
throw new NoSuchElementException();
}
return linked.remove(size()-1);
}

public boolean isEmpty() {
if(size()-1 == 0) {
return true;
} else {
return false;
}
}

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

@Override
public String toString() {
return linked.toString();
}
}
Loading

0 comments on commit c9fcb66

Please sign in to comment.