-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from langmanba/master
'第一次作業'
- Loading branch information
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |