diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SDEV333-Term-Project-main-2 2/SDEV333-Term-Project.iml b/SDEV333-Term-Project-main-2 2/SDEV333-Term-Project.iml new file mode 100644 index 0000000..aeefa0f --- /dev/null +++ b/SDEV333-Term-Project-main-2 2/SDEV333-Term-Project.iml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SDEV333-Term-Project-main-2 2/src/ArrayList.java b/SDEV333-Term-Project-main-2 2/src/ArrayList.java new file mode 100644 index 0000000..c981047 --- /dev/null +++ b/SDEV333-Term-Project-main-2 2/src/ArrayList.java @@ -0,0 +1,221 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + + + +public class ArrayList implements List{ + private static final int BUFFER_CAPACITY=10;// + private Object[] elements;//array to store elements + private int size;// current size of the list + + //constructor to initialize arraylist + public ArrayList(){ + elements = new Object[BUFFER_CAPACITY];//initialize the array with default capacity + size=0;// setting size to 0 + } + /** + * Add item to the front. + * + * @param item the item to be added + */ + + //o(n) + //because it involves shifting all elements to right and has to make space for new item + @Override + public void addFront(E item) { +add(0, item);// add to insert at index 0 + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + + //o(1)-adding an elements to the end of array + @Override + public void addBack(E item) { +if(size== elements.length){// check if the array is full +throw new IllegalArgumentException("arraylist is full"); + +} +elements[size++]=item;// adding the item at the end of list + } + + /** + * Add an item at specified index (position). + * + * @param index the index where the item should be added + * @param item the item to be added + */ + + //o(n)worst case -have to shift the elements to right by one position + @Override + public void add(int index, E item) { +if(index<0||index>size){ + throw new IndexOutOfBoundsException("index out of range"); + +} +if(size==elements.length){ + throw new IllegalArgumentException("array full"); + +} +//shifting elements to the right to make space for the new item + for (int i =size; i >index; i--){ + elements[i]=elements[i-1]; + } + elements[index]=item;// adding item at an index +size++;// + } + + /** + * Get the item at a specified index. + * + * @param index the index where the item should be retrieved + * @return the item located at that index + */ + //O(1) this directly accesses the elements at the specified index + @Override + public E get(int index) { + if(index<0||index>=size){ + throw new IndexOutOfBoundsException("index out of range"); + } + return (E) elements[index]; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param index the index where the item should be saved + * @param item the item to be saved + */ + @Override + public void set(int index, E item) { +if(index<0||index>=size){ + throw new IndexOutOfBoundsException("out of range"); + +} +elements[index]=item;// set the item at specified index + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + return remove(0);// to remove method to remove from index 0 + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + return remove(size-1);//remove form last index + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { +for(int i=0; i =size){ + throw new IndexOutOfBoundsException("out of range"); + + } + E removedItem =(E) elements[index];// store the item to remove + for (int i = index; i iterator() { + return new Iterator() { + private int currentIndex=0;// + @Override + public boolean hasNext(){ + return currentIndex implements List { + + //node + private class Node { + E data; + Node next; + } + + + private Node head;// head node of the linked list + //size of the linked list + private int size; + + //constructor + public LinkedListt() { + head = null; + size = 0; + } + + /** + * Add item to the front. + * + * @param item the item to be added + */ + + //O(1)adds new node at starting of list + @Override + public void addFront(E item) { + Node newNode = new Node();// new node + newNode.data = item; + newNode.next = head; + head = newNode; + size++; + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + //o(n) worst case + @Override + public void addBack(E item) { + Node newNode = new Node(); + newNode.data = item; + if (head == null) {// if list empty + head = newNode;//set head to new node + } else { + Node current = head;// + while (current.next != null) {//to last node + current = current.next; + } + current.next = newNode; + } + size++; + } + + /** + * Add an item at specified index (position). + * + * @param i the index where the item should be added + * @param item the item to be added + */ + //o(n)need to terverse the linked list to find node at specififed index before addin new node + @Override + public void add(int i, E item) {// + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException(); + + } + if (i == 0) { + addFront(item); + return; + } + Node newNode = new Node(); + newNode.data = item; + Node current = head; + for (int index = 0; index < i - 1; index++) { + current = current.next; + } + newNode.next = current.next;// next of new node to next of current + current.next = newNode; + size++; + } + + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + + //0(n)? + @Override + public E get(int i) { + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException(); + + } + Node current = head; + for (int index = 0; index < i; index++) { + current = current.next; + } + return current.data; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param i the index where the item should be saved + * @param item the item to be saved + */ + // + @Override + public void set(int i, E item) { + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException(); + + } + Node current = head; + for (int index = 0; index < i; index++) { + current = current.next; + } + current.data = item; + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + //0(1)it removes the first node form list + @Override + public E removeFront() { + if (isEmpty()) { + throw new NoSuchElementException(); + + } + E data = head.data; + head = head.next; + size--; + return data; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() {// if empty + if (isEmpty()) { + throw new NoSuchElementException(); + + } + if (size == 1) {//if only one left in list + return removeFront(); + + } + Node prev = null; + Node current = head; + while (current.next != null) { + prev = current; + current = current.next; + } + prev.next = null; + size--; + return current.data; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + //0() + @Override + public void remove(E item) { + if (isEmpty()) { + throw new NoSuchElementException(); + } + if (head.data.equals(item)) { + head = head.next; + size--; + return; + } + Node prev = null; + Node current = head; + while (current != null && !current.data.equals(item)) { + prev = current; + current = current.next; + } + if (current == null) { + throw new NoSuchElementException(); + + } + prev.next = current.next; + size--; + } + + /** + * Remove item at a specified index. + * + * @param i the index where the item should be removed + * @return the item that was removed + */ + // + @Override + public E remove(int i) { + if (i < 0 || i >= size) { + + } + if (i == 0) { + return removeFront(); + } + Node prev = null; + Node current = head; + for (int index = 0; + index < i; index++) { + prev = current; + current = current.next; + } + prev.next = current.next; + size--; + return current.data; + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + Node current = head; + while (current != null) { + + if (current.data.equals(item)) { + return true; + } + current = current.next; + } + return false; + } + + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + //o(1) this checks size if it is zero + @Override + public boolean isEmpty() { + return size==0; + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + //0(1)returns the size of the list + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new Iterator() { + private Node current = head; + @Override + public boolean hasNext() { + return current!=null; + } + + @Override + public E next() { + if(!hasNext()){ + throw new NoSuchElementException(); + } + E data = current.data; + current=current.next; + return data; + } + }; + } +} diff --git a/src/List.java b/SDEV333-Term-Project-main-2 2/src/List.java similarity index 100% rename from src/List.java rename to SDEV333-Term-Project-main-2 2/src/List.java diff --git a/src/Main.java b/SDEV333-Term-Project-main-2 2/src/Main.java similarity index 100% rename from src/Main.java rename to SDEV333-Term-Project-main-2 2/src/Main.java diff --git a/src/MathSet.java b/SDEV333-Term-Project-main-2 2/src/MathSet.java similarity index 100% rename from src/MathSet.java rename to SDEV333-Term-Project-main-2 2/src/MathSet.java diff --git a/src/Queue.java b/SDEV333-Term-Project-main-2 2/src/Queue.java similarity index 100% rename from src/Queue.java rename to SDEV333-Term-Project-main-2 2/src/Queue.java diff --git a/src/Stack.java b/SDEV333-Term-Project-main-2 2/src/Stack.java similarity index 100% rename from src/Stack.java rename to SDEV333-Term-Project-main-2 2/src/Stack.java diff --git a/SDEV333-Term-Project-main-2 2/src/test.java b/SDEV333-Term-Project-main-2 2/src/test.java new file mode 100644 index 0000000..e61a68f --- /dev/null +++ b/SDEV333-Term-Project-main-2 2/src/test.java @@ -0,0 +1,152 @@ +import org.junit.jupiter.api.Test; +import java.util.Iterator; + + +import static org.junit.jupiter.api.Assertions.*; + +// test class for ArrayList +public class test { + @Test + public void testAddBackGetArray() { + // to check adding elements to back and getting them + ArrayList list = new ArrayList<>();//instance + list.addBack(1);// adding 1 to list + list.addBack(2);// + list.addBack(3); + assertEquals(3, list.size());// + assertEquals(1, list.get(0)); + assertEquals(2, list.get(1)); + assertEquals(3, list.get(2)); + + } + + //test case to check adding elemets to the front and removing them + @Test + public void testAddFrontRemoveArray() { + ArrayList list = new ArrayList<>(); + list.addFront("pink"); + list.addFront("red"); + + assertEquals(2, list.size());// lis should be 2 + assertEquals("red", list.removeFront()); + assertEquals(1, list.size()); + assertEquals("pink", list.get(0)); + } + + // check removing an elements by index + @Test + public void TestRemoveByIndexArray() { + ArrayList list = new ArrayList<>(); + //add to list + list.addBack('a'); + list.addBack('b'); + list.addBack('c'); + + assertEquals('b', list.remove(1));// At index 1 it is "b" + assertEquals(2, list.size()); + assertFalse(list.contains('b')); + + } + + //check if the list is empty + @Test + public void testISEmptyArray() { + ArrayList list = new ArrayList<>(); + assertTrue(list.isEmpty()); + list.addBack(3.14);// addd to list + assertFalse(list.isEmpty()); + list.removeBack(); + assertTrue(list.isEmpty()); + } + + + @Test + public void testIteratorArray() { + ArrayList list = new ArrayList<>(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + Iterator iterator = list.iterator(); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertEquals(2, iterator.next()); + assertEquals(3, iterator.next()); + assertFalse(iterator.hasNext()); + + } + + + + + + //LinkedList + + // test for adding an item to front of empty list + @Test + public void testAddFrontEmptyLinkedList() { + LinkedListt list = new LinkedListt<>();// empty linked list + list.addFront(1);// add front + assertEquals(1, list.size()); + assertEquals(Integer.valueOf(1), list.get(0)); + } + +//adding to the back of list + @Test + public void testAddBackGetLinkedList() { + LinkedListt list = new LinkedListt<>(); + list.addBack(1);// add back + list.addBack(2); + list.addBack(3); + assertEquals(3, list.size());//updated + assertEquals(Integer.valueOf(1), list.get(0));// check if done correctly + assertEquals(Integer.valueOf(2), list.get(1)); + assertEquals(Integer.valueOf(3), list.get(2)); + + } + + //test for adding an item to front and then remove + @Test + public void testAddFrontRemoveLinkedList(){ + LinkedListtlist= new LinkedListt<>(); + list.addFront("apple");// add front + list.addFront("mango"); + assertEquals(2, list.size());// check + assertEquals("mango",list.removeFront());// remove first + assertEquals(1,list.size());// updated? + assertEquals("apple",list.get(0)); + } + + //remove by index + @Test + public void testRemovedByIndexLinkedList(){ + LinkedListtlist=new LinkedListt<>();// + list.addBack('a');//add back + list.addBack('b'); + list.addBack('c'); + assertEquals(Character.valueOf('b'), list.remove(1));// remove by index + assertEquals(2, list.size()); + assertFalse(list.contains('b')); + } + //to check if linked list empty + @Test + public void testIsEmptyLinkedList(){ + LinkedListtlist=new LinkedListt<>(); + assertTrue(list.isEmpty()); + list.addBack(3.14); + } + @Test + public void testIteratorLinkedList(){ + LinkedListtlist=new LinkedListt<>(); + list.addBack(1); + list.addBack(2); + list.addBack(3); + Iteratoriterator=list.iterator();// iterator for list + assertTrue(iterator.hasNext());// check for next + assertEquals(Integer.valueOf(1),iterator.next());// if correct + assertTrue(iterator.hasNext());// check for next + assertEquals(Integer.valueOf(2),iterator.next()); + assertTrue(iterator.hasNext()); + assertEquals(Integer.valueOf(3),iterator.next()); + assertFalse(iterator.hasNext()); + } + }