-
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 #7 from Weiyin-Chen/master
assignment 1 draft
- Loading branch information
Showing
7 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
group20/331798361/assignment1/src/com/coding/basic/ArrayList.java
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,122 @@ | ||
package com.coding.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o){ | ||
int n = elementData.length; | ||
int i = 0; | ||
while (elementData[i] != null) { | ||
i++; | ||
} | ||
if (i < n) { | ||
elementData[i] = o; | ||
} else { | ||
Object[] temp = Arrays.copyOf(elementData, n + 1); | ||
temp[n] = o; | ||
elementData = temp; | ||
} | ||
size++; | ||
} | ||
public void add(int index, Object o){ | ||
int n = elementData.length; | ||
int i = 0; | ||
while (elementData[i] != null) { | ||
i++; | ||
} | ||
if (index < 0 || index > i) { | ||
System.out.println(index + " is invalid index!"); | ||
return; | ||
} | ||
if (i < n) { | ||
for (int j = i; j > index; j--) { | ||
elementData[j] = elementData[j - 1]; | ||
} | ||
elementData[index] = o; | ||
} else { | ||
Object[] temp = Arrays.copyOf(elementData, n + 1); | ||
for (int j = i; j > index; j--) { | ||
temp[j] = temp[j - 1]; | ||
} | ||
temp[index] = o; | ||
elementData = temp; | ||
} | ||
size++; | ||
} | ||
|
||
public Object get(int index){ | ||
int i = 0; | ||
while (elementData[i] != null) { | ||
i++; | ||
} | ||
if (index < 0 || index >= i) { | ||
System.out.println(index + " is invalid index!"); | ||
return null; | ||
} else { | ||
return elementData[index]; | ||
} | ||
} | ||
|
||
public Object remove(int index){ | ||
int i = 0; | ||
while (elementData[i] != null) { | ||
i++; | ||
} | ||
if (index < 0 || index >= i) { | ||
System.out.println(index + " is invalid index!"); | ||
return null; | ||
} | ||
Object result = elementData[index]; | ||
for (int j = index; j < i; j++) { | ||
elementData[j] = elementData[j + 1]; | ||
} | ||
size--; | ||
return result; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public String toString() { | ||
int i = 0; | ||
while (elementData[i] != null) { | ||
i++; | ||
} | ||
String result = ""; | ||
for (int j = 0; j < i - 1; j++) { | ||
result += elementData[j].toString() + ", "; | ||
} | ||
result += elementData[size - 1].toString(); | ||
return result; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
public static void main(String args[]){ | ||
ArrayList list1 = new ArrayList(); | ||
list1.add("a"); | ||
list1.add("b"); | ||
list1.add("c"); | ||
System.out.println(list1.toString()); | ||
list1.add(5, "d"); | ||
list1.add(1, "d"); | ||
System.out.println(list1.toString()); | ||
list1.add(4, "e"); | ||
System.out.println(list1.toString()); | ||
list1.get(5); | ||
System.out.println(list1.get(0)); | ||
list1.remove(2); | ||
System.out.println(list1.toString()); | ||
System.out.println(list1.size()); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java
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,32 @@ | ||
package com.coding.basic; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o){ | ||
return null; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
group20/331798361/assignment1/src/com/coding/basic/Iterator.java
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,7 @@ | ||
package com.coding.basic; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
148 changes: 148 additions & 0 deletions
148
group20/331798361/assignment1/src/com/coding/basic/LinkedList.java
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,148 @@ | ||
package com.coding.basic; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
private int size; | ||
|
||
public void add(Object o){ | ||
if (head.data == null) { | ||
head.data = o; | ||
head.next = null; | ||
size++; | ||
return; | ||
} | ||
Node node = new Node(o); | ||
Node curr = head; | ||
while (curr.next != null) { | ||
curr = curr.next; | ||
} | ||
curr.next = node; | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
|
||
if (index < 0 || index > size) { | ||
System.out.println(index + " is invalid index!"); | ||
return; | ||
} | ||
if (head.data == null) { | ||
if (index == 0) { | ||
head.data = o; | ||
head.next = null; | ||
size++; | ||
return; | ||
} else { | ||
System.out.println("invalid index!"); | ||
return; | ||
} | ||
} | ||
Node node = new Node(o); | ||
Node curr = head; | ||
for (int i = 0; i < index - 1; i++) { | ||
curr = curr.next; | ||
} | ||
Node temp = curr.next; | ||
curr.next = node; | ||
node.next = temp; | ||
size++; | ||
} | ||
public Object get(int index){ | ||
if (index < 0 || index > size) { | ||
System.out.println(index + " is invalid index!"); | ||
return null; | ||
} | ||
Node result = head; | ||
for (int i = 0; i < index; i++) { | ||
result = result.next; | ||
} | ||
return result; | ||
} | ||
public Object remove(int index){ | ||
if (index < 0 || index > size) { | ||
System.out.println(index + " is invalid index!"); | ||
return null; | ||
} | ||
Node curr = head; | ||
for (int i = 0; i < index - 1; i++) { | ||
curr = curr.next; | ||
} | ||
Node result = curr.next; | ||
curr.next = curr.next.next; | ||
size--; | ||
return result; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
if (head.data == null) { | ||
head.data = o; | ||
head.next = null; | ||
size++; | ||
return; | ||
} | ||
Node temp = head; | ||
head = new Node(o); | ||
head.next = temp; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o){ | ||
if (head.data == null) { | ||
head.data = o; | ||
head.next = null; | ||
size++; | ||
return; | ||
} | ||
Node node = new Node(o); | ||
Node curr = head; | ||
while (curr.next != null) { | ||
curr = curr.next; | ||
} | ||
curr.next = node; | ||
size++; | ||
} | ||
|
||
public Object removeFirst(){ | ||
if (head.data == null) { | ||
return null; | ||
} | ||
Node result = head; | ||
head = head.next; | ||
size--; | ||
return result; | ||
} | ||
|
||
public Object removeLast(){ | ||
if (head.data == null) { | ||
return null; | ||
} | ||
Node curr = head; | ||
for (int i = 0; i < size - 1; i++) { | ||
curr = curr.next; | ||
} | ||
Node result = curr.next; | ||
curr.next = null; | ||
size--; | ||
return result; | ||
} | ||
|
||
public Iterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
Node(Object o) { | ||
data = o; | ||
next = null; | ||
} | ||
|
||
} | ||
} |
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,9 @@ | ||
package com.coding.basic; | ||
|
||
public interface List { | ||
public void add(Object o); | ||
public void add(int index, Object o); | ||
public Object get(int index); | ||
public Object remove(int index); | ||
public int size(); | ||
} |
30 changes: 30 additions & 0 deletions
30
group20/331798361/assignment1/src/com/coding/basic/Queue.java
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 com.coding.basic; | ||
|
||
|
||
public class Queue { | ||
private LinkedList list = new LinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
list.add(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
int length = list.size(); | ||
if (length == 0) { | ||
return null; | ||
} | ||
return list.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if (list.size() == 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return list.size(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
group20/331798361/assignment1/src/com/coding/basic/Stack.java
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,37 @@ | ||
package com.coding.basic; | ||
|
||
public class Stack { | ||
private ArrayList elementData = new ArrayList(); | ||
|
||
public void push(Object o) { | ||
elementData.add(o); | ||
} | ||
|
||
public Object pop(){ | ||
int length = elementData.size(); | ||
if (length == 0) { | ||
return null; | ||
} | ||
return elementData.remove(length - 1); | ||
} | ||
|
||
public Object peek(){ | ||
int length = elementData.size(); | ||
if (length == 0) { | ||
return null; | ||
} | ||
return elementData.get(length - 1); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if (elementData.size() != 0) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
public int size(){ | ||
return elementData.size(); | ||
} | ||
} |