Skip to content

Commit 9f9c4d9

Browse files
committed
assignment 1 draft
1 parent b300f43 commit 9f9c4d9

File tree

7 files changed

+385
-0
lines changed

7 files changed

+385
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.coding.basic;
2+
3+
import java.util.Arrays;
4+
5+
public class ArrayList implements List {
6+
7+
8+
private int size = 0;
9+
10+
private Object[] elementData = new Object[100];
11+
12+
public void add(Object o){
13+
int n = elementData.length;
14+
int i = 0;
15+
while (elementData[i] != null) {
16+
i++;
17+
}
18+
if (i < n) {
19+
elementData[i] = o;
20+
} else {
21+
Object[] temp = Arrays.copyOf(elementData, n + 1);
22+
temp[n] = o;
23+
elementData = temp;
24+
}
25+
size++;
26+
}
27+
public void add(int index, Object o){
28+
int n = elementData.length;
29+
int i = 0;
30+
while (elementData[i] != null) {
31+
i++;
32+
}
33+
if (index < 0 || index > i) {
34+
System.out.println(index + " is invalid index!");
35+
return;
36+
}
37+
if (i < n) {
38+
for (int j = i; j > index; j--) {
39+
elementData[j] = elementData[j - 1];
40+
}
41+
elementData[index] = o;
42+
} else {
43+
Object[] temp = Arrays.copyOf(elementData, n + 1);
44+
for (int j = i; j > index; j--) {
45+
temp[j] = temp[j - 1];
46+
}
47+
temp[index] = o;
48+
elementData = temp;
49+
}
50+
size++;
51+
}
52+
53+
public Object get(int index){
54+
int i = 0;
55+
while (elementData[i] != null) {
56+
i++;
57+
}
58+
if (index < 0 || index >= i) {
59+
System.out.println(index + " is invalid index!");
60+
return null;
61+
} else {
62+
return elementData[index];
63+
}
64+
}
65+
66+
public Object remove(int index){
67+
int i = 0;
68+
while (elementData[i] != null) {
69+
i++;
70+
}
71+
if (index < 0 || index >= i) {
72+
System.out.println(index + " is invalid index!");
73+
return null;
74+
}
75+
Object result = elementData[index];
76+
for (int j = index; j < i; j++) {
77+
elementData[j] = elementData[j + 1];
78+
}
79+
size--;
80+
return result;
81+
}
82+
83+
public int size(){
84+
return size;
85+
}
86+
87+
public String toString() {
88+
int i = 0;
89+
while (elementData[i] != null) {
90+
i++;
91+
}
92+
String result = "";
93+
for (int j = 0; j < i - 1; j++) {
94+
result += elementData[j].toString() + ", ";
95+
}
96+
result += elementData[size - 1].toString();
97+
return result;
98+
}
99+
100+
public Iterator iterator(){
101+
return null;
102+
}
103+
104+
public static void main(String args[]){
105+
ArrayList list1 = new ArrayList();
106+
list1.add("a");
107+
list1.add("b");
108+
list1.add("c");
109+
System.out.println(list1.toString());
110+
list1.add(5, "d");
111+
list1.add(1, "d");
112+
System.out.println(list1.toString());
113+
list1.add(4, "e");
114+
System.out.println(list1.toString());
115+
list1.get(5);
116+
System.out.println(list1.get(0));
117+
list1.remove(2);
118+
System.out.println(list1.toString());
119+
System.out.println(list1.size());
120+
}
121+
122+
}
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: 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: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.coding.basic;
2+
3+
public class LinkedList implements List {
4+
5+
private Node head;
6+
private int size;
7+
8+
public void add(Object o){
9+
if (head.data == null) {
10+
head.data = o;
11+
head.next = null;
12+
size++;
13+
return;
14+
}
15+
Node node = new Node(o);
16+
Node curr = head;
17+
while (curr.next != null) {
18+
curr = curr.next;
19+
}
20+
curr.next = node;
21+
size++;
22+
}
23+
public void add(int index , Object o){
24+
25+
if (index < 0 || index > size) {
26+
System.out.println(index + " is invalid index!");
27+
return;
28+
}
29+
if (head.data == null) {
30+
if (index == 0) {
31+
head.data = o;
32+
head.next = null;
33+
size++;
34+
return;
35+
} else {
36+
System.out.println("invalid index!");
37+
return;
38+
}
39+
}
40+
Node node = new Node(o);
41+
Node curr = head;
42+
for (int i = 0; i < index - 1; i++) {
43+
curr = curr.next;
44+
}
45+
Node temp = curr.next;
46+
curr.next = node;
47+
node.next = temp;
48+
size++;
49+
}
50+
public Object get(int index){
51+
if (index < 0 || index > size) {
52+
System.out.println(index + " is invalid index!");
53+
return null;
54+
}
55+
Node result = head;
56+
for (int i = 0; i < index; i++) {
57+
result = result.next;
58+
}
59+
return result;
60+
}
61+
public Object remove(int index){
62+
if (index < 0 || index > size) {
63+
System.out.println(index + " is invalid index!");
64+
return null;
65+
}
66+
Node curr = head;
67+
for (int i = 0; i < index - 1; i++) {
68+
curr = curr.next;
69+
}
70+
Node result = curr.next;
71+
curr.next = curr.next.next;
72+
size--;
73+
return result;
74+
}
75+
76+
public int size(){
77+
return size;
78+
}
79+
80+
public void addFirst(Object o){
81+
if (head.data == null) {
82+
head.data = o;
83+
head.next = null;
84+
size++;
85+
return;
86+
}
87+
Node temp = head;
88+
head = new Node(o);
89+
head.next = temp;
90+
size++;
91+
}
92+
93+
public void addLast(Object o){
94+
if (head.data == null) {
95+
head.data = o;
96+
head.next = null;
97+
size++;
98+
return;
99+
}
100+
Node node = new Node(o);
101+
Node curr = head;
102+
while (curr.next != null) {
103+
curr = curr.next;
104+
}
105+
curr.next = node;
106+
size++;
107+
}
108+
109+
public Object removeFirst(){
110+
if (head.data == null) {
111+
return null;
112+
}
113+
Node result = head;
114+
head = head.next;
115+
size--;
116+
return result;
117+
}
118+
119+
public Object removeLast(){
120+
if (head.data == null) {
121+
return null;
122+
}
123+
Node curr = head;
124+
for (int i = 0; i < size - 1; i++) {
125+
curr = curr.next;
126+
}
127+
Node result = curr.next;
128+
curr.next = null;
129+
size--;
130+
return result;
131+
}
132+
133+
public Iterator iterator(){
134+
return null;
135+
}
136+
137+
138+
private static class Node{
139+
Object data;
140+
Node next;
141+
142+
Node(Object o) {
143+
data = o;
144+
next = null;
145+
}
146+
147+
}
148+
}
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.coding.basic;
2+
3+
4+
public class Queue {
5+
private LinkedList list = new LinkedList();
6+
7+
public void enQueue(Object o){
8+
list.add(o);
9+
}
10+
11+
public Object deQueue(){
12+
int length = list.size();
13+
if (length == 0) {
14+
return null;
15+
}
16+
return list.removeFirst();
17+
}
18+
19+
public boolean isEmpty(){
20+
if (list.size() == 0) {
21+
return true;
22+
} else {
23+
return false;
24+
}
25+
}
26+
27+
public int size(){
28+
return list.size();
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.coding.basic;
2+
3+
public class Stack {
4+
private ArrayList elementData = new ArrayList();
5+
6+
public void push(Object o) {
7+
elementData.add(o);
8+
}
9+
10+
public Object pop(){
11+
int length = elementData.size();
12+
if (length == 0) {
13+
return null;
14+
}
15+
return elementData.remove(length - 1);
16+
}
17+
18+
public Object peek(){
19+
int length = elementData.size();
20+
if (length == 0) {
21+
return null;
22+
}
23+
return elementData.get(length - 1);
24+
}
25+
26+
public boolean isEmpty(){
27+
if (elementData.size() != 0) {
28+
return false;
29+
} else {
30+
return true;
31+
}
32+
}
33+
34+
public int size(){
35+
return elementData.size();
36+
}
37+
}

0 commit comments

Comments
 (0)