Skip to content

Commit 22d30e2

Browse files
committed
441908378 week1 homework
1 parent 1595986 commit 22d30e2

File tree

5 files changed

+287
-0
lines changed

5 files changed

+287
-0
lines changed

group12/441908378/ArrayList.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.Arrays;
2+
3+
public class ArrayList {
4+
5+
private int size = 0;
6+
7+
private Object[] elementData = new Object[100];
8+
9+
public void enlargeCapacity(int minCapacity){
10+
int oldCapacity=elementData.length;
11+
if(oldCapacity<minCapacity){
12+
oldCapacity=(oldCapacity*3)/2+1;
13+
}
14+
}
15+
16+
public void add(Object o){
17+
enlargeCapacity(elementData.length+1);
18+
elementData[elementData.length+1]=o;
19+
}
20+
public void add(int index, Object o){
21+
enlargeCapacity(elementData.length+1);
22+
Object data[]= Arrays.copyOfRange(elementData, index, elementData.length);
23+
elementData[index] = o;
24+
System.arraycopy(data, 0, elementData, index+1, elementData.length);
25+
26+
}
27+
28+
public Object get(int index){
29+
Object o;
30+
o=elementData[index];
31+
return o;
32+
}
33+
34+
public Object remove(int index){
35+
Object o;
36+
o=elementData[index];
37+
Object data[]= Arrays.copyOfRange(elementData, index+1, elementData.length);
38+
System.arraycopy(data, 0, elementData, index, elementData.length);
39+
return o;
40+
}
41+
42+
public int size(){
43+
return size;
44+
}
45+
46+
//public Iterator iterator(){
47+
//return null;
48+
//}
49+
50+
}

group12/441908378/BinaryTreeNode.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
public class BinaryTreeNode {
3+
4+
private Object data;
5+
private BinaryTreeNode left;
6+
private BinaryTreeNode right;
7+
8+
public Object getData() {
9+
return data;
10+
}
11+
public void setData(Object data) {
12+
this.data = data;
13+
}
14+
public BinaryTreeNode getLeft() {
15+
return left;
16+
}
17+
public void setLeft(BinaryTreeNode left) {
18+
this.left = left;
19+
}
20+
public BinaryTreeNode getRight() {
21+
return right;
22+
}
23+
public void setRight(BinaryTreeNode right) {
24+
this.right = right;
25+
}
26+
27+
public BinaryTreeNode insert(Object o){
28+
BinaryTreeNode node;
29+
do{
30+
node=compare(o);
31+
}while(node==null);
32+
node.data=o;
33+
return node;
34+
}
35+
36+
public BinaryTreeNode compare(Object o){
37+
int a=(Integer)data;
38+
int b=(Integer)o;
39+
if(a>b){
40+
return left;
41+
}else{
42+
return right;
43+
}
44+
}
45+
46+
47+
}

group12/441908378/LinkedList.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
public class LinkedList {
2+
3+
private Node head;
4+
5+
private static class Node{
6+
Object data;
7+
Node next;
8+
}
9+
10+
public boolean hasNext(Node a){
11+
if(a.next!=null){
12+
return true;
13+
}
14+
return false;
15+
}
16+
17+
public Node getIndex(int index){
18+
Node a=head.next;
19+
for(int i=0;i<index;i++){
20+
a=a.next;
21+
}
22+
return a;
23+
}
24+
25+
public void add(Object o){
26+
Node a;
27+
if(!hasNext(head)){
28+
a=head.next;
29+
a.data=o;
30+
}else{
31+
a=head.next;
32+
while(a.next!=null){
33+
a=a.next;
34+
}
35+
a=a.next;
36+
a.data=o;
37+
}
38+
}
39+
40+
41+
public void add(int index , Object o){
42+
Node a=getIndex(index);
43+
Node b=null;
44+
b.data=o;
45+
b.next=a.next;
46+
a.next=b;
47+
}
48+
public Object get(int index){
49+
Object o;
50+
Node a=getIndex(index);
51+
o=a.data;
52+
return o;
53+
}
54+
public Object remove(int index){
55+
Node a=getIndex(index-1);
56+
Node b=a.next;
57+
Object c=b.data;
58+
a.next=b.next;
59+
b.next=null;
60+
return c;
61+
}
62+
63+
public int size(){
64+
Node a=head.next;
65+
int i=0;
66+
while(a.next==null){
67+
a=a.next;
68+
i++;
69+
}
70+
return i;
71+
}
72+
73+
public void addFirst(Object o){
74+
Node a=null;
75+
a.data=o;
76+
Node b=head.next;
77+
a.next=b;
78+
head.next=a;
79+
}
80+
public void addLast(Object o){
81+
Node a;
82+
if(!hasNext(head)){
83+
a=head.next;
84+
a.data=o;
85+
}else{
86+
a=head.next;
87+
while(a.next!=null){
88+
a=a.next;
89+
}
90+
a=a.next;
91+
a.data=o;
92+
}
93+
}
94+
public Object removeFirst(){
95+
Node a=head.next;
96+
Node b=a.next;
97+
Object c=a.data;
98+
head.next=b;
99+
a.next=null;
100+
return c;
101+
}
102+
public Object removeLast(){
103+
Node a=head.next;
104+
while(a.next.next!=null){
105+
a=a.next;
106+
};
107+
Object b=a.next.data;
108+
a.next=null;
109+
return b;
110+
111+
}
112+
113+
114+
//public Iterator iterator(){
115+
//return null;
116+
//}
117+
118+
119+
120+
121+
}

group12/441908378/Queue.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Arrays;
2+
3+
4+
public class Queue {
5+
6+
7+
private int size = 0;
8+
9+
private Object[] elementData = new Object[100];
10+
11+
public void enlargeCapacity(int minCapacity){
12+
int oldCapacity=elementData.length;
13+
if(oldCapacity<minCapacity){
14+
oldCapacity=(oldCapacity*3)/2+1;
15+
}
16+
}
17+
18+
public void enQueue(Object o){
19+
enlargeCapacity(elementData.length+1);
20+
elementData[elementData.length+1]=o;
21+
}
22+
23+
public Object deQueue(){
24+
Object a = elementData[0];
25+
elementData=Arrays.copyOfRange(elementData, 1, elementData.length);
26+
return null;
27+
}
28+
29+
public boolean isEmpty(){
30+
if(elementData.length==0){
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
public int size(){
37+
return elementData.length;
38+
}
39+
}

group12/441908378/Stack.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
public class Stack {
3+
private ArrayList elementData = new ArrayList();
4+
5+
private int size = 0;
6+
7+
public void push(Object o){
8+
elementData.add(o);
9+
}
10+
11+
public Object pop(){
12+
Object o=elementData.get(elementData.size());
13+
elementData.remove(elementData.size());
14+
return o;
15+
}
16+
17+
public Object peek(){
18+
Object o=elementData.get(elementData.size());
19+
return o;
20+
}
21+
public boolean isEmpty(){
22+
if(elementData.size()==0){
23+
return true;
24+
}
25+
return false;
26+
}
27+
public int size(){
28+
return elementData.size();
29+
}
30+
}

0 commit comments

Comments
 (0)