Skip to content

Commit da249e7

Browse files
Merge pull request onlyliuxin#3 from khalil2333/master
Master
2 parents 2a69b5e + 58f30c7 commit da249e7

File tree

9 files changed

+346
-0
lines changed

9 files changed

+346
-0
lines changed

group03/1753176091/bin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/com/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayList implements List {
6+
7+
private int size = 0;
8+
9+
private Object[] elementData = new Object[100];
10+
11+
public void add(Object o) {
12+
ensureCapacity(size + 1);
13+
elementData[size++] = o;
14+
}
15+
16+
private void ensureCapacity(int minCapacity) {
17+
if (minCapacity > 100) {
18+
grow(elementData);
19+
}
20+
}
21+
22+
private void grow(Object[] elementData) {
23+
int oldLength = elementData.length;
24+
int newLength = oldLength * 2;
25+
Arrays.copyOf(elementData, newLength);
26+
}
27+
28+
public void add(int index, Object o) {
29+
ensureCapacity(size + 1);
30+
System.arraycopy(elementData, index, elementData, index + 1, size - index);
31+
elementData[index] = o;
32+
}
33+
34+
public Object get(int index) {
35+
if (index < 0 || index > elementData.length) {
36+
throw new IndexOutOfBoundsException();
37+
}
38+
return (Object) elementData[index];
39+
}
40+
41+
public Object remove(int index) {
42+
int lowLength = size - index - 1;
43+
System.arraycopy(elementData, index + 1, elementData, index, lowLength);
44+
elementData[--size] = null;
45+
return (Object) elementData[index];
46+
}
47+
48+
public int size() {
49+
return size;
50+
}
51+
52+
public Iterator iterator() {
53+
return null;
54+
}
55+
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coding.basic;
2+
3+
public class BinaryTreeNode {
4+
5+
private Object data;
6+
private BinaryTreeNode left;
7+
private BinaryTreeNode right;
8+
9+
public Object getData() {
10+
return data;
11+
}
12+
public void setData(Object data) {
13+
this.data = data;
14+
}
15+
public BinaryTreeNode getLeft() {
16+
return left;
17+
}
18+
public void setLeft(BinaryTreeNode left) {
19+
this.left = left;
20+
}
21+
public BinaryTreeNode getRight() {
22+
return right;
23+
}
24+
public void setRight(BinaryTreeNode right) {
25+
this.right = right;
26+
}
27+
28+
public BinaryTreeNode insert(Object o){
29+
return null;
30+
}
31+
32+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coding.basic;
2+
3+
4+
5+
import java.io.BufferedInputStream;
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
11+
public class FileUtil {
12+
13+
public static String byteToHexString(byte[] codes ){
14+
15+
StringBuffer buffer = new StringBuffer();
16+
17+
for(int i=0;i<codes.length;i++){
18+
byte b = codes[i];
19+
int value = b & 0xFF;
20+
String strHex = Integer.toHexString(value);
21+
if(strHex.length()< 2){
22+
strHex = "0" + strHex;
23+
}
24+
buffer.append(strHex);
25+
}
26+
return buffer.toString();
27+
}
28+
29+
public static byte[] readByteCodes(String clzFileName) throws IOException {
30+
31+
File f = new File(clzFileName);
32+
33+
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
34+
35+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
36+
37+
38+
byte[] buffer = new byte[1024];
39+
int length = -1;
40+
try {
41+
while((length = bis.read(buffer)) != -1){
42+
bos.write(buffer, 0, length);
43+
}
44+
} catch (IOException e) {
45+
e.printStackTrace();
46+
throw e;
47+
} finally{
48+
if(bis != null){
49+
bis.close();
50+
}
51+
if(bos !=null){
52+
bos.close();
53+
}
54+
}
55+
56+
byte [] codes = bos.toByteArray();
57+
58+
bis.close();
59+
60+
return codes;
61+
}
62+
public static void main(String[] args) throws IOException{
63+
byte[] codes = FileUtil.readByteCodes("C:\\coderising\\workspace_ds\\Warmup\\bin\\FileUtil.class");;
64+
65+
String hexCodes = FileUtil.byteToHexString(codes);
66+
System.out.println(hexCodes);
67+
}
68+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.coding.basic;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class LinkedList implements List {
6+
7+
private Node head;
8+
private int size;
9+
10+
public void add(Object o) {
11+
12+
Node newHead = new Node(o, null);
13+
Node f = head;
14+
for (int i = 0; i < size - 1; i++) {
15+
f = f.next;
16+
}
17+
f.next = newHead;
18+
size++;
19+
20+
}
21+
22+
public void add(int index, Object o) {
23+
if (index >= size || index < 0) {
24+
throw new IndexOutOfBoundsException();
25+
}
26+
Node newHead = new Node(o, null);
27+
Node f = head;
28+
for (int i = 0; i < index - 1; i++) {
29+
f = f.next;
30+
}
31+
newHead.next = f.next;
32+
f.next = newHead;
33+
size++;
34+
}
35+
36+
public Object get(int index) {
37+
if (index >= size || index < 0) {
38+
throw new IndexOutOfBoundsException();
39+
}
40+
Node f = head;
41+
for (int i = 0; i < index; i++) {
42+
f = f.next;
43+
}
44+
return f.data;
45+
}
46+
47+
public Object remove(int index) {
48+
if (index >= size || index < 0) {
49+
throw new IndexOutOfBoundsException();
50+
}
51+
Node f = head;
52+
for (int i = 0; i < index - 1; i++) {
53+
f = f.next;
54+
}
55+
f.next = f.next.next;
56+
final Node d = f.next;
57+
final Object element = d.data;
58+
d.data = null;
59+
d.next = null;
60+
size--;
61+
return element;
62+
}
63+
64+
public int size() {
65+
return size;
66+
}
67+
68+
public void addFirst(Object o) {
69+
Node newHead = new Node(o, head);
70+
head = newHead;
71+
size++;
72+
}
73+
74+
public void addLast(Object o) {
75+
Node newHead = new Node(o, null);
76+
Node f = head;
77+
for (int i = 0; i < size - 1; i++) {
78+
f = f.next;
79+
}
80+
f.next = newHead;
81+
size++;
82+
}
83+
84+
public Object removeFirst() {
85+
final Node f = head;
86+
if (f == null)
87+
throw new NoSuchElementException();
88+
final Object element = f.data;
89+
head = f.next;
90+
f.data = null;
91+
f.next = null;
92+
size--;
93+
return element;
94+
}
95+
96+
public Object removeLast() {
97+
Node f = head;
98+
for (int i = 0; i < size - 2; i++) {
99+
f = f.next;
100+
}
101+
Object element = f.next;
102+
f.next = null;
103+
size--;
104+
return element;
105+
}
106+
107+
public Iterator iterator() {
108+
return null;
109+
}
110+
111+
private static class Node {
112+
Object data;
113+
Node next;
114+
115+
private Node(Object data, Node next) {
116+
this.data = data;
117+
this.next = next;
118+
}
119+
}
120+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coding.basic;
2+
3+
public interface List {
4+
public void add(Object o);
5+
public void add(int index, Object o);
6+
public Object get(int index);
7+
public Object remove(int index);
8+
public int size();
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.coding.basic;
2+
3+
public class Queue {
4+
5+
private int size;
6+
LinkedList elementData = new LinkedList();
7+
8+
public void enQueue(Object o) {
9+
elementData.addLast(o);
10+
size++;
11+
}
12+
13+
public Object deQueue() {
14+
size--;
15+
return elementData.removeFirst();
16+
}
17+
18+
public boolean isEmpty() {
19+
return size == 0;
20+
}
21+
22+
public int size() {
23+
return size;
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
private int size = 0;
6+
7+
public void push(Object o) {
8+
elementData.add(o);
9+
}
10+
11+
public Object pop() {
12+
if (size > 0)
13+
return elementData.remove(size);
14+
return null;
15+
}
16+
17+
public Object peek() {
18+
return elementData.get(size);
19+
}
20+
21+
public boolean isEmpty() {
22+
return size == 0;
23+
}
24+
25+
public int size() {
26+
return size;
27+
}
28+
}

0 commit comments

Comments
 (0)