Skip to content

Commit

Permalink
Merge pull request #5 from hwjcc969/master
Browse files Browse the repository at this point in the history
第一次作业
  • Loading branch information
Rong Huang authored Feb 25, 2017
2 parents dfa4596 + 0b24942 commit b0fb337
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github.hwjcc969.coding2017.basic;

import javax.lang.model.element.Element;

public class ArrayList implements List {

private int size = 0;

private Object[] elementData = new Object[100];

public void add(Object o){
if (o == null){
throw new NullPointerException("空对象");
}
if (size >= 100){
throw new RuntimeException("越界");
}
elementData[size] = o;
size++;
}
public void add(int index, Object o){
if (o == null){
throw new NullPointerException("空对象");
}
if (index >= 99){
throw new RuntimeException("越界");
}
for (int i = size; i >= index; i--) {
elementData[i + 1] = elementData[i];
}
elementData[index] = o;
size++;
}

public Object get(int index){
if (index >= 100 || index < 0){
throw new RuntimeException("越界");
}
return elementData[index];
}

public Object remove(int index){
if (index >= 100 || index < 0){
throw new RuntimeException("越界");
}
Object o = elementData[index];
for (int i = index; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
size--;
return null;
}

public int size(){
return size;
}

public Iterator iterator(){
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.hwjcc969.coding2017.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,7 @@
package com.github.hwjcc969.coding2017.basic;

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

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

public class LinkedList implements List {

private Node head;
private int size;

public void add(Object o){
Node newNode = new Node(o);
if (head == null) {
head = newNode;
}
else{
Node last = head.next;
while (last != null){
last = last.next;
}
last = newNode;
}
size++;

}
public void add(int index , Object o){
Node dummy = new Node();
dummy.next = head;
Node pre = dummy;
while (index > 0) {
pre = pre.next;
}
Node cur = new Node(o);
cur.next = pre.next;
pre.next = cur;
size++;
}
public Object get(int index){
Node cur = head;
while (index > 0 && cur != null) {
cur = cur.next;
}
return cur;
}
public Object remove(int index){
Node dummy = new Node();
dummy.next = head;
Node pre = dummy;
while (index > 0) {
pre = pre.next;
}
Node cur = pre.next;
Node next = pre.next.next;
pre.next = next;
size--;
return cur;

}

public int size(){
return size;
}

public void addFirst(Object o){
Node firstNode = new Node(o);
firstNode.next = head;
head = firstNode;
}
public void addLast(Object o){
Node pre = new Node();
while (pre.next != null){
pre = pre.next;
}
Node lastNode = new Node(o);
pre.next = lastNode;
}
public Object removeFirst(){
Node dummy = new Node();
dummy.next = head;
dummy.next = head.next;
return dummy.next;
}
public Object removeLast(){
Node pre = new Node();
while (pre.next.next != null) {
pre = pre.next;
}
Node lastNode = pre.next.next;
pre.next = null;
return lastNode;

}
public Iterator iterator(){
return null;
}


private static class Node{
public Node(Object o) {
// TODO Auto-generated constructor stub
}
public Node() {
// TODO Auto-generated constructor stub
}
Object data;
Node next;

}


public boolean isEmpty() {
// TODO Auto-generated method stub
if (size() == 0){
return false;
}
return true;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.hwjcc969.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,27 @@
package com.github.hwjcc969.coding2017.basic;

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

public Object deQueue(){
if (!list.isEmpty()){
return list.removeFirst();
}
return "空队列";

}

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

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

public class Stack {
private ArrayList elementData = new ArrayList();
private int size = 0;

public void push(Object o){
if (elementData.size() <= 99)
elementData.add(o);
size++;

}

public Object pop(){
size--;
return elementData.remove(elementData.size() - 1);

}

public Object peek(){
return elementData.get(elementData.size() - 1);
}
public boolean isEmpty(){
if (size == 0)
return false;
return true;
}
public int size(){
return size;
}
}

0 comments on commit b0fb337

Please sign in to comment.