From b8d2634e89a27e4d19b46a4200cf56de34a056f9 Mon Sep 17 00:00:00 2001 From: =gogoloda <=gogoloda@gmail.com> Date: Sat, 25 Feb 2017 10:54:12 -0600 Subject: [PATCH] Commit my new homework files except the Iterator to my master --- group04/564451732/.classpath | 7 ++ group04/564451732/.gitignore | 1 + group04/564451732/.project | 17 +++ group04/564451732/src/DataStructureTest.java | 114 +++++++++++++++++ group04/564451732/src/Test.java | 8 ++ group04/564451732/src/hw1/ArrayListImpl.java | 88 +++++++++++++ group04/564451732/src/hw1/BinaryTreeNode.java | 71 +++++++++++ group04/564451732/src/hw1/Iterator.java | 7 ++ group04/564451732/src/hw1/LinkedList.java | 117 ++++++++++++++++++ group04/564451732/src/hw1/List.java | 9 ++ group04/564451732/src/hw1/QueueImpl.java | 21 ++++ group04/564451732/src/hw1/StackImpl.java | 23 ++++ 12 files changed, 483 insertions(+) create mode 100644 group04/564451732/.classpath create mode 100644 group04/564451732/.gitignore create mode 100644 group04/564451732/.project create mode 100644 group04/564451732/src/DataStructureTest.java create mode 100644 group04/564451732/src/Test.java create mode 100644 group04/564451732/src/hw1/ArrayListImpl.java create mode 100644 group04/564451732/src/hw1/BinaryTreeNode.java create mode 100644 group04/564451732/src/hw1/Iterator.java create mode 100644 group04/564451732/src/hw1/LinkedList.java create mode 100644 group04/564451732/src/hw1/List.java create mode 100644 group04/564451732/src/hw1/QueueImpl.java create mode 100644 group04/564451732/src/hw1/StackImpl.java diff --git a/group04/564451732/.classpath b/group04/564451732/.classpath new file mode 100644 index 0000000000..2bdfdc1bf0 --- /dev/null +++ b/group04/564451732/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group04/564451732/.gitignore b/group04/564451732/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group04/564451732/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group04/564451732/.project b/group04/564451732/.project new file mode 100644 index 0000000000..23af4c7791 --- /dev/null +++ b/group04/564451732/.project @@ -0,0 +1,17 @@ + + + 564451732 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group04/564451732/src/DataStructureTest.java b/group04/564451732/src/DataStructureTest.java new file mode 100644 index 0000000000..2f5cc27a94 --- /dev/null +++ b/group04/564451732/src/DataStructureTest.java @@ -0,0 +1,114 @@ +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import hw1.ArrayListImpl; +import hw1.BinaryTreeNode; +import hw1.LinkedList; +import hw1.QueueImpl; +import hw1.StackImpl; + +public class DataStructureTest { + + @Test + public void ArrayListTest() { + ArrayListImpl al = new ArrayListImpl(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + al.add(new Object()); + al.add(new Object()); + assertTrue(al.size() == 2); + al.add(5); + assertFalse(al.size() == 2); + assertTrue(al.size() == 3); + al.add(2, 3); + //System.out.println((int)al.get(2)); + assertTrue((int)al.get(2) == 3); + assertTrue((int)al.get(3) == 5); + } + + @Test + public void LinkedListTest() { + LinkedList ll = new LinkedList(); + Object o1 = new Object(); + Object o2 = new Object(); + Object o3 = new Object(); + Object o4 = new Object(); + Object o5 = new Object(); + Object o6 = new Object(); + ll.add(o1); + ll.add(o2); + //System.out.println(ll.size()); + assertTrue(ll.size() == 2); + ll.add(o3); + ll.add(1,1); + assertTrue((int)ll.get(1) == 1); + assertTrue(ll.size() == 4); + ll.addFirst(2); + ll.addLast(4); + assertTrue((int)ll.get(0) == 2); + assertTrue((int)ll.get(ll.size() - 1) == 4); + assertTrue((int)ll.get(2) == 1); + ll.remove(2); + assertTrue(ll.get(2) == o2); + //System.out.print((int)ll.remove(2)); + //System.out.println((int)ll.get(2)); + //assertFalse((int)ll.get(2) == 1); + assertTrue(ll.size() == 5); + assertTrue((int)ll.removeFirst() == 2); + assertTrue(ll.size() == 4); + assertTrue((int)ll.removeLast() == 4); + assertTrue(ll.size() == 3); + } + + @Test + public void QueueTest(){ + QueueImpl qi = new QueueImpl(); + assertTrue(qi.isEmpty()); + qi.enQueue(1); + qi.enQueue(2); + qi.enQueue(3); + assertTrue(qi.size() == 3); + assertTrue((int)qi.deQueue() == 1); + assertTrue(qi.size() == 2); + assertFalse(qi.isEmpty()); + } + + @Test + public void StackTest() { + StackImpl stack = new StackImpl(); + assertTrue(stack.isEmpty()); + stack.push(1); + stack.push(2); + stack.push(3); + assertTrue(stack.size() == 3); + assertTrue((int)stack.pop() == 3); + assertTrue(stack.size() == 2); + assertTrue((int)stack.peek() == 2); + assertFalse(stack.isEmpty()); + } + + @Test + public void BinaryTreeTest() { + BinaryTreeNode bt = new BinaryTreeNode(); + Integer i1 = 1; + Integer i2 = 3; + Integer i3 = 4; + Integer i4 = 6; + Integer i5 = -1; + bt.insert(i3); + bt.insert(i1); + bt.insert(i4); + bt.insert(i2); + bt.insert(i5); + assertTrue((int)bt.getRoot().getData() == 4); + assertTrue((int)bt.getRoot().getLeft().getData() == 1); + assertTrue((int)bt.getRoot().getRight().getData() == 6); + assertTrue((int)bt.getRoot().getLeft().getLeft().getData() == -1); + assertTrue((int)bt.getRoot().getLeft().getRight().getData() == 3); + + } + +} diff --git a/group04/564451732/src/Test.java b/group04/564451732/src/Test.java new file mode 100644 index 0000000000..3335242a80 --- /dev/null +++ b/group04/564451732/src/Test.java @@ -0,0 +1,8 @@ + +public class Test { + + public static void main(String[] args) { + + } + +} diff --git a/group04/564451732/src/hw1/ArrayListImpl.java b/group04/564451732/src/hw1/ArrayListImpl.java new file mode 100644 index 0000000000..fe42bfc4f7 --- /dev/null +++ b/group04/564451732/src/hw1/ArrayListImpl.java @@ -0,0 +1,88 @@ +package hw1; + +//import java.util.ArrayList; +import java.util.Iterator; +//import java.util.List; + +import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet; + +public class ArrayListImpl implements List { + +private int size = 0; + + private Object[] elementData = new Object[100]; + + public void add(Object o){ + //size++; + try{ + if (size < elementData.length) { + elementData[size] = o; + } else { + elementData = grow(elementData, elementData.length); + elementData[size] = o; + } + size++; + } catch (IndexOutOfBoundsException e) { + System.out.println("The added index is out of bound"); + } + } + public void add(int index, Object o){ + if (index < 0) { + throw new IndexOutOfBoundsException("Index cannot be less than 0"); + } + while (index >= elementData.length) { + elementData = grow(elementData, elementData.length); + } + if (index >= size) { + elementData[index] = o; + size++; + } else { + if (size + 1 >= elementData.length) { + elementData = grow(elementData, elementData.length); + } + for (int i = size-1; i >= index;i--) { + elementData[i+1] = elementData[i]; + } + elementData[index] = o; + size++; + + } + } + + public Object get(int index){ + if (index >= size ||index < 0) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } else { + return elementData[index]; + } + + } + + public Object remove(int index){ + if (index >= size ||index < 0) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } else { + Object result = elementData[index]; + for (int i = index; i < size - 1; i++) { + elementData[i] = elementData[i+1]; + } + elementData[size - 1] = 0; + size--; + return result; + } + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return null; + } + + private Object[] grow (Object[] src, int increase) { + Object[] target = new Object[src.length + increase]; + System.arraycopy(src, 0, target, 0, src.length); + return target; + } +} diff --git a/group04/564451732/src/hw1/BinaryTreeNode.java b/group04/564451732/src/hw1/BinaryTreeNode.java new file mode 100644 index 0000000000..4b0367f13c --- /dev/null +++ b/group04/564451732/src/hw1/BinaryTreeNode.java @@ -0,0 +1,71 @@ +package hw1; + +public class BinaryTreeNode { + private Comparable data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private BinaryTreeNode root; + + public BinaryTreeNode (Comparable data) { + this.data = data; + } + public BinaryTreeNode () { + + } + public Object getData() { + return data; + } + public void setData(Comparable 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; + return; + } + + + + public BinaryTreeNode getRoot() { + return root; + } + public void setRoot(BinaryTreeNode root) { + this.root = root; + } + public BinaryTreeNode insert(Comparable o){ + BinaryTreeNode result = new BinaryTreeNode(o); + if (root == null) { + root = result; + } else { + BinaryTreeNode dummy = root; + insertHelper(o,dummy); + + } + return result; + } + + private void insertHelper (Comparable o, BinaryTreeNode root) { + if (o.compareTo(root.data) < 0) { + if (root.left == null) { + root.left = new BinaryTreeNode(o); + } else { + insertHelper (o, root.left); + } + } else { + if (root.right == null) { + root.right = new BinaryTreeNode(o); + + } else { + insertHelper (o, root.right); + } + } + } +} diff --git a/group04/564451732/src/hw1/Iterator.java b/group04/564451732/src/hw1/Iterator.java new file mode 100644 index 0000000000..93e22e9377 --- /dev/null +++ b/group04/564451732/src/hw1/Iterator.java @@ -0,0 +1,7 @@ +package hw1; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + +} diff --git a/group04/564451732/src/hw1/LinkedList.java b/group04/564451732/src/hw1/LinkedList.java new file mode 100644 index 0000000000..793f7c66d6 --- /dev/null +++ b/group04/564451732/src/hw1/LinkedList.java @@ -0,0 +1,117 @@ +package hw1; + +import java.util.Iterator; + +public class LinkedList implements List { +private Node head; +private Node tail; + + public void add(Object o){ + if (head != null) { + Node dummy = new Node(new Object()); + dummy.next = head; + Node nextNode = head; + + while (nextNode != null) { + dummy = dummy.next; + nextNode = nextNode.next; + } + dummy.next = new Node(o); + tail = dummy.next; + } else { + head = new Node(o); + tail = head; + } + + } + public void add(int index , Object o){ + if (index < 0 || index > size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 1; i < index; i++) { + dummy = dummy.next; + } + Node temp = dummy.next; + dummy.next = new Node(o); + dummy.next.next = temp; + if (index == size()) { + tail = dummy.next; + } + } + public Object get(int index){ + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 0; i < index;i++) { + dummy = dummy.next; + } + return dummy.data; + } + public Object remove(int index){ + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("Index entered is out of bounds"); + } + Node dummy = head; + for (int i = 1; i < index;i++) { + dummy = dummy.next; + } + Node result = dummy.next; + dummy.next = dummy.next.next; + if (dummy.next == null) { + tail = dummy; + } + return result.data; + } + + public int size(){ + Node dummy = head; + int size = 0; + while (dummy != null) { + dummy = dummy.next; + size++; + } + + return size; + } + + public void addFirst(Object o){ + Node first = new Node(o); + first.next = head; + head = first; + } + public void addLast(Object o){ + tail.next = new Node(o); + tail = tail.next; + } + public Object removeFirst(){ + Node temp = head; + head = head.next; + return temp.data; + } + public Object removeLast(){ + Node dummy = head; + for (int i = 1; i < size()-1; i++) { + dummy = dummy.next; + } + Node result = dummy.next; + tail = dummy; + dummy.next = null; + return result.data; + } + public Iterator iterator(){ + return null; + } + + + private static class Node{ + Object data; + Node next; + + public Node(Object data) { + this.data = data; + } + + } +} diff --git a/group04/564451732/src/hw1/List.java b/group04/564451732/src/hw1/List.java new file mode 100644 index 0000000000..a976c82204 --- /dev/null +++ b/group04/564451732/src/hw1/List.java @@ -0,0 +1,9 @@ +package hw1; + +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/group04/564451732/src/hw1/QueueImpl.java b/group04/564451732/src/hw1/QueueImpl.java new file mode 100644 index 0000000000..30c73585ff --- /dev/null +++ b/group04/564451732/src/hw1/QueueImpl.java @@ -0,0 +1,21 @@ +package hw1; + +public class QueueImpl { + LinkedList queueList = new LinkedList(); + + public void enQueue(Object o){ + queueList.add(o); + } + + public Object deQueue(){ + return queueList.removeFirst(); + } + + public boolean isEmpty(){ + return queueList.size() == 0; + } + + public int size(){ + return queueList.size(); + } +} diff --git a/group04/564451732/src/hw1/StackImpl.java b/group04/564451732/src/hw1/StackImpl.java new file mode 100644 index 0000000000..3ae23593fa --- /dev/null +++ b/group04/564451732/src/hw1/StackImpl.java @@ -0,0 +1,23 @@ +package hw1; + +public class StackImpl { +private ArrayListImpl elementData = new ArrayListImpl(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + public boolean isEmpty(){ + return elementData.size() == 0; + } + public int size(){ + return elementData.size(); + } +}