Skip to content

Commit

Permalink
Merge pull request #9 from langmanba/master
Browse files Browse the repository at this point in the history
'第一次作業'
  • Loading branch information
Mr-ChangK authored Mar 13, 2017
2 parents 54f1f53 + 46fb00a commit d5a4f1e
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
49 changes: 49 additions & 0 deletions group23/609041842/src/code/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package code;

import java.util.Arrays;

public class ArrayList {

private Object[] obj = new Object[0];

public void add(Object o) {
Object[] tar = new Object[obj.length + 1];
System.arraycopy(obj, 0, tar, 0, obj.length);
tar[tar.length - 1] = o;
obj = tar;
System.out.println(Arrays.toString(obj));
}

public void add(int index, Object o) {
Object[] tar = new Object[obj.length + 1];
System.arraycopy(obj, 0, tar, 0, index);
tar[index] = o;
System.arraycopy(obj, index, tar, index + 1, obj.length - index);
obj = tar;
}

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

public int size(){
return obj.length;
}

public Object remove(int index){
Object[] tar = new Object[obj.length-1];
System.arraycopy(obj, 0, tar, 0, index);
System.arraycopy(obj, index+1, tar, index, obj.length-index-1);
Object o = obj[index];
obj = tar;
return o;//·µ»Ø±»É¾ÔªËØ
}
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("hello");
al.add("java");
al.add(2, "addm");
System.out.println(al.remove(1));
}

}
87 changes: 87 additions & 0 deletions group23/609041842/src/code/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package code;

public class LinkedList {
private static Node head;
private Node last;
public int size;

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

public void add(int index, Object o) {
Node n = node(index);
System.out.println(n.data);
Node pred = n.prev;
Node newNode = new Node(pred, o, n);
if (pred == null) {
head = newNode;
} else {
pred.next = newNode;
}
size++;
}


public Node get(int index){
return node(index);
}
public Node node(int index) {
Node n = head;
for (int i = 0; i < index; i++) {
n = n.next;
}
return n;
}

public Node remove(int index){
Node del = node(index);
Node after = del.next;
Node before = del.prev;
before.next = after;
after.prev = before;
size--;
return del;
}
private static class Node {
Node next;
Object data;
Node prev;

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

public static void main(String[] arg) {
LinkedList ll = new LinkedList();
ll.add("hello");
ll.add("java");
ll.add("jvm");
ll.add("jvmd");
// System.out.println(ll.get(2));
// ll.add(1, "ds");
System.out.println(ll.get(0).data);
System.out.println(ll.get(1).data);
System.out.println(ll.get(2).data);
System.out.println(ll.get(3).data);
System.out.println(ll.size);
System.out.println(ll.remove(1).data);
System.out.println(ll.get(0).data);
System.out.println(ll.get(1).data);
System.out.println(ll.get(2).data);
System.out.println(ll.size);
}

}
17 changes: 17 additions & 0 deletions group23/609041842/src/code/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package code;

public class Queue {

private LinkedList lk = new LinkedList();
public void enQueue(Object o){
lk.add(o);
}
public void deQueue(){
lk.remove(lk.size-1);
}
public boolean isEmpty(){
if(lk.size == 0)
return true;
return false;
}
}
30 changes: 30 additions & 0 deletions group23/609041842/src/code/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package code;

public class Stack {

private ArrayList array = new ArrayList();

public void push(Object o){
array.add(o);
}
public Object pop(){
return array.remove(array.size()-1);
}

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

public int size(){
return array.size();
}
public static void main(String[] args) {
Stack sc = new Stack();
sc.push("hello world");
sc.push("java");
sc.push("jvm");
}

}

0 comments on commit d5a4f1e

Please sign in to comment.