diff --git a/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..0d960ee3d8 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/ArrayList.java @@ -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()); + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..266eff3d56 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/BinaryTreeNode.java @@ -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; + } + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Iterator.java b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..dbe8b9afb2 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..e029beaeb4 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/LinkedList.java @@ -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; + } + + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/List.java b/group20/331798361/assignment1/src/com/coding/basic/List.java new file mode 100644 index 0000000000..396b1f6416 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/List.java @@ -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(); +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Queue.java b/group20/331798361/assignment1/src/com/coding/basic/Queue.java new file mode 100644 index 0000000000..c4991a07f5 --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/Queue.java @@ -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(); + } +} diff --git a/group20/331798361/assignment1/src/com/coding/basic/Stack.java b/group20/331798361/assignment1/src/com/coding/basic/Stack.java new file mode 100644 index 0000000000..0a48545cea --- /dev/null +++ b/group20/331798361/assignment1/src/com/coding/basic/Stack.java @@ -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(); + } +}