simo před 1 měsícem
revize
b9ae5f57da

+ 1 - 0
README.md

@@ -0,0 +1 @@
+All of my labs from CSCI 258

+ 109 - 0
calculatorAlgorithm/Main.java

@@ -0,0 +1,109 @@
+import java.lang.Integer;
+import java.util.Scanner;
+import java.util.Stack;
+
+class Main {
+
+    public static void main(String args[]) {
+        Scanner input = new Scanner(System.in);
+
+        System.out.print("Enter Equation : ");
+
+        String postfix = infixToPostfix(input.nextLine());
+
+        System.out.println(solve(postfix));
+
+        input.close();
+    }
+
+    public static int solve(String postfix) {
+        Stack<Integer> ints = new Stack<>();
+
+        for (int i = 0; i < postfix.length(); i++) {
+            char x = postfix.charAt(i);
+            if (x == ' ') {
+                continue;
+            } else if (Character.isDigit(x)) {
+                String fullNumber = "";
+
+                while (postfix.charAt(i) != ' ') {
+                    fullNumber += postfix.charAt(i);
+                    i++;
+                }
+
+                ints.push(Integer.parseInt(fullNumber));
+            } else {
+                int num2 = ints.pop();
+                int num1 = ints.pop();
+
+                switch (x) {
+                    case '/':
+                        ints.push(num1 / num2);
+                        break;
+                    case '*':
+                        ints.push(num1 * num2);
+                        break;
+                    case '+':
+                        ints.push(num1 + num2);
+                        break;
+                    case '-':
+                        ints.push(num1 - num2);
+                        break;
+                }
+            }
+        }
+
+        return ints.pop();
+    }
+
+    public static Integer calculatePrecedence(Character c) {
+        if (c == '*' || c == '/') {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+
+    public static String infixToPostfix(String infix) {
+        Stack<Character> stack = new Stack<>();
+
+        String postfix = "";
+
+        int i = 0;
+
+        while (i < infix.length()) {
+            char c = infix.charAt(i);
+
+            if (Character.isDigit(c)) {
+                String num = "";
+
+                while (
+                    (i < infix.length()) && Character.isDigit(infix.charAt(i))
+                ) {
+                    num += infix.charAt(i);
+
+                    i++;
+                }
+
+                postfix += num + " ";
+            } else {
+                while (
+                    !stack.empty() &&
+                    calculatePrecedence(stack.peek()) >= calculatePrecedence(c)
+                ) {
+                    postfix += stack.pop();
+                }
+
+                stack.push(c);
+
+                i++;
+            }
+        }
+
+        while (!stack.empty()) {
+            postfix += stack.pop();
+        }
+
+        return postfix;
+    }
+}

+ 155 - 0
circularArrayLab/SimpleArrayList.java

@@ -0,0 +1,155 @@
+import java.util.NoSuchElementException;
+
+/**
+ * An implementation of the SimpleList interface using a circular array.
+ *
+ * @param <E> component type
+ */
+public class SimpleArrayList<E> implements SimpleList<E> {
+
+    // Main method to test the addFirst method.
+    // Add to this to test other methods.
+    public static void main(String[] args) {
+        SimpleList<Integer> list = new SimpleArrayList<Integer>(20);
+        for (int i = 2; i <= 40; i += 2) list.addFirst(i);
+        System.out.println(list);
+    }
+
+    private E[] store; // Array where list's elements are stored.
+    private int head; // Index of the first element in the list
+    private int tail; // Index of next available array cell at end of list
+    private int size; // Number of elements in the list
+
+    /**
+     * Capacity constructor - creates an empty list with an
+     * array of the given capacity.
+     *
+     * @param initialCapacity initial capacity (length) of the array
+     */
+    public SimpleArrayList(int initialCapacity) {
+        store = (E[]) new Object[initialCapacity];
+    }
+
+    /**
+     * Default constructor - creates an empty list with an
+     * array of capacity 10.
+     */
+    public SimpleArrayList() {
+        this(10);
+    }
+
+    @Override
+    /**
+     * Adds the given element to the start of the list.
+     * (Increases array capacity if array is full.)
+     * @param elem element to add
+     */
+    public void addFirst(E elem) {
+        if (size == store.length) increaseCapacity();
+        head = (head - 1 + store.length) % store.length;
+        store[head] = elem;
+        size++;
+    }
+
+    @Override
+    /**
+     * Removes the last element from the list.
+     * @return element that was removed
+     * @throws NoSuchElementException if there is no last element
+     */
+    public E removeLast() {
+        if (size == 0) throw new NoSuchElementException("list empty");
+        tail = (tail - 1 + store.length) % store.length;
+        E doomed = store[tail];
+        size--;
+        return doomed;
+    }
+
+    @Override
+    /**
+     * Returns the element in the list at the given index.
+     *
+     * (NOTE: The given index is the list index, not the
+     * array index.)
+     *
+     * @param index index of element to return
+     * @return element at given index
+     * @throws IndexOutOfBoundsException if given index is invalid
+     */
+    public E get(int index) {
+        if (index < 0 || index >= size) throw new IndexOutOfBoundsException(
+            "invalid index"
+        );
+        int arrayIndex = (head + index) % store.length;
+        return store[arrayIndex];
+    }
+
+    @Override
+    /**
+     * Returns true of this list is equal to the given list.
+     * Two lists are equal if they contain the same elements in the same order.
+     * @param otherList list to compare to this list
+     * @return true if lists are equal
+     */
+    public boolean equals(SimpleList<E> otherList) {
+        if (otherList instanceof SimpleArrayList) {
+            SimpleArrayList<E> otherArrayList = (SimpleArrayList<E>) otherList;
+
+            if (this.size != otherArrayList.size) return false;
+            for (int i = 0; i < size; i++) {
+                E thisEl = this.get(i);
+                E otherEl = otherArrayList.get(i);
+                if (!thisEl.equals(otherEl)) return false;
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public void addLast(E elem) {
+        if (size == store.length) increaseCapacity();
+        store[tail] = elem;
+        tail = (tail + 1) % store.length;
+        size++;
+    }
+
+    @Override
+    public E removeFirst() {
+        if (size == 0) throw new NoSuchElementException("list empty");
+        E doomed = store[head];
+        head = (head + 1) % store.length;
+        size--;
+        return doomed;
+    }
+
+    @Override
+    public int size() {
+        return size;
+    }
+
+    @Override
+    public String toString() {
+        if (size == 0) return "[]";
+        else {
+            String s = "[";
+            for (
+                int i = 0, j = head;
+                i < size;
+                i++, j = (j + 1) % store.length
+            ) s += store[j] + ", ";
+            return s.substring(0, s.length() - 2) + "]";
+        }
+    }
+
+    private void increaseCapacity() {
+        E[] bigger = (E[]) new Object[size + size / 2];
+        for (int i = 0; i < size; i++) {
+            bigger[i] = store[head];
+            head = (head + 1) % size;
+        }
+        head = 0;
+        tail = size;
+        store = bigger;
+    }
+}

+ 63 - 0
circularArrayLab/SimpleList.java

@@ -0,0 +1,63 @@
+/**
+ * This interface describes a simple list with operations
+ * to add and remove elements from either end of the list.
+ *
+ * @param <E> type of elements stored in the list
+ */
+public interface SimpleList<E> {
+    /**
+     * Adds the given element to the start of the list.
+     * @param elem element to add
+     */
+    public void addFirst(E elem);
+
+    /**
+     * Adds the given element to the end of the list.
+     * @param elem element to add
+     */
+    public void addLast(E elem);
+
+    /**
+     * Removes the first element from the list.
+     * @return element that was removed
+     * @throws NoSuchElementException if there is no first element
+     */
+    public E removeFirst();
+
+    /**
+     * Removes the last element from the list.
+     * @return element that was removed
+     * @throws NoSuchElementException if there is no last element
+     */
+    public E removeLast();
+
+    /**
+     * Returns the size of the list (i.e. the number of elements
+     * stored in the list.
+     * @return size of list
+     */
+    public int size();
+
+    /**
+     * Returns the element in the list at the given index.
+     * @param index index of element to return
+     * @return element at given index
+     * @throws IndexOutOfBoundsException if given index is invalid
+     */
+    public E get(int index);
+
+    /**
+     * Returns the list as a string. The string contains all of the elements
+     * in order from first to last.
+     * @return list as a string
+     */
+    public String toString();
+
+    /**
+     * Returns true of this list is equal to the given list.
+     * Two lists are equal if they contain the same elements in the same order.
+     * @param otherList list to compare to this list
+     * @return true if lists are equal
+     */
+    public boolean equals(SimpleList<E> otherList);
+}

+ 103 - 0
linkedListLab/LinkedList.java

@@ -0,0 +1,103 @@
+/**
+ * An implementation of the Linked List data structure.
+ *
+ * The LinkedList is like the ArrayList, except the list's
+ * elements are stored in linked nodes instead of an array.
+ *
+ * @param <E> component type
+ */
+public class LinkedList<E> {
+
+    public static void main(String[] args) {
+        LinkedList<Integer> list = new LinkedList<>();
+        for (int i = 2; i <= 20; i += 2) list.add(i);
+        System.out.println(list);
+        System.out.println(list.get(4)); // should print 10
+    }
+
+    // The list's elements are stored in nodes.
+    private class Node {
+
+        E element; // Element stored in this node
+        Node next; // Link to the next node in the list
+    }
+
+    private Node head; // Link to the first node
+    private Node tail; // Link to the last node
+    private int size; // Number of elements in the list
+
+    public void add(E elem) {
+        Node n = new Node();
+        n.element = elem;
+        if (head == null) head = tail = n;
+        else {
+            tail.next = n;
+            tail = n;
+        }
+        size++;
+    }
+
+    public E get(int index) {
+        if (index < 0 || index >= size) throw new IndexOutOfBoundsException(
+            index + ""
+        );
+        Node walker = head;
+        for (int i = 0; i < index; i++) walker = walker.next;
+        return walker.element;
+    }
+
+    public void set(int index, E element) {
+        if (index < 0 || index >= size) throw new IndexOutOfBoundsException(
+            index + ""
+        );
+        Node walker = head;
+        for (int i = 0; i < index; i++) walker = walker.next;
+
+        walker.element = element;
+    }
+
+    public E get(E element) {
+        Node walker = head;
+        for (int i = 0; i < size; i++) {
+            if (walker.element.equals(element)) {
+                return walker.element;
+            }
+
+            walker = walker.next;
+        }
+
+        return null;
+    }
+
+    public boolean contains(E element) {
+        Node walker = head;
+        for (int i = 0; i < size; i++) {
+            if (walker.element.equals(element)) {
+                return true;
+            }
+
+            walker = walker.next;
+        }
+
+        return false;
+    }
+
+    public int size() {
+        return size;
+    }
+
+    @Override
+    public String toString() {
+        if (head == null) return "[]";
+        else {
+            String s = "[";
+            Node walker = head;
+            while (walker != tail) {
+                s += walker.element + ", ";
+                walker = walker.next;
+            }
+            s += tail.element + "]";
+            return s;
+        }
+    }
+}

+ 33 - 0
linkedStackLab/LinkedStack.java

@@ -0,0 +1,33 @@
+public class LinkedStack<E> {
+
+    Node top;
+
+    class Node {
+
+        E element;
+        Node next;
+    }
+
+    public void push(E element) {
+        Node newNode = new Node();
+        newNode.element = element;
+        newNode.next = top;
+        top = newNode;
+    }
+
+    public E pop() {
+        if (top == null) {
+            return null;
+        }
+        E data = top.element;
+        top = top.next;
+        return data;
+    }
+
+    public E peek() {
+        if (top == null) {
+            return null;
+        }
+        return top.element;
+    }
+}

+ 82 - 0
simpleList/SimpleLinkedList.java

@@ -0,0 +1,82 @@
+import java.util.NoSuchElementException;
+
+public class SimpleLinkedList<E> implements SimpleList<E> {
+
+    private class Node {
+
+        E element;
+        Node next;
+    }
+
+    private Node head;
+    private Node tail;
+    private int size;
+
+    @Override
+    public void addFirst(E elem) {
+        Node n = new Node();
+        n.element = elem;
+        if (head == null) {
+            head = tail = n;
+        } else {
+            n.next = head;
+            head = n;
+        }
+
+        size++;
+    }
+
+    @Override
+    public E removeFirst() {
+        if (head == null) {
+            return null;
+        }
+
+        E element = head.element;
+        head = head.next;
+
+        if (head == null) {
+            tail = null;
+        }
+
+        size--;
+        return element;
+    }
+
+    @Override
+    public E removeLast() {
+        if (head == null) {
+            throw new NoSuchElementException("Empty List");
+        } else {
+            E element = tail.element;
+            if (size == 1) {
+                head = tail = null;
+            } else {
+                Node walker = head;
+                while (walker.next != tail) {
+                    walker = walker.next;
+                }
+
+                walker.next = null;
+                tail = walker;
+            }
+
+            size--;
+            return element;
+        }
+    }
+
+    @Override
+    public void addLast(E elem) {
+        Node n = new Node();
+        n.element = elem;
+        if (tail == null) {
+            head = tail = n;
+        } else {
+            tail.next = n;
+            tail = n;
+        }
+
+        size++;
+    }
+}

+ 63 - 0
simpleList/SimpleList.java

@@ -0,0 +1,63 @@
+/**
+ * This interface describes a simple list with operations
+ * to add and remove elements from either end of the list.
+ *
+ * @param <E> type of elements stored in the list
+ */
+public interface SimpleList<E> {
+    /**
+     * Adds the given element to the start of the list.
+     * @param elem element to add
+     */
+    public void addFirst(E elem);
+
+    /**
+     * Adds the given element to the end of the list.
+     * @param elem element to add
+     */
+    public void addLast(E elem);
+
+    /**
+     * Removes the first element from the list.
+     * @return element that was removed
+     * @throws NoSuchElementException if there is no first element
+     */
+    public E removeFirst();
+
+    /**
+     * Removes the last element from the list.
+     * @return element that was removed
+     * @throws NoSuchElementException if there is no last element
+     */
+    public E removeLast();
+
+    /**
+     * Returns the size of the list (i.e. the number of elements
+     * stored in the list.
+     * @return size of list
+     */
+    public int size();
+
+    /**
+     * Returns the element in the list at the given index.
+     * @param index index of element to return
+     * @return element at given index
+     * @throws IndexOutOfBoundsException if given index is invalid
+     */
+    public E get(int index);
+
+    /**
+     * Returns the list as a string. The string contains all of the elements
+     * in order from first to last.
+     * @return list as a string
+     */
+    public String toString();
+
+    /**
+     * Returns true of this list is equal to the given list.
+     * Two lists are equal if they contain the same elements in the same order.
+     * @param otherList list to compare to this list
+     * @return true if lists are equal
+     */
+    public boolean equals(SimpleList<E> otherList);
+}

+ 56 - 0
stackLab/Main.java

@@ -0,0 +1,56 @@
+import java.util.Stack;
+
+public class Main {
+
+    public static void main(String[] args) {
+        System.out.print("Enter a string: ");
+        java.util.Scanner scanner = new java.util.Scanner(System.in);
+        String input = scanner.nextLine();
+
+        Boolean isPalindrome = palindrome(input);
+
+        String not = "";
+
+        if (!isPalindrome) {
+            not = "not ";
+        }
+
+        System.out.println(input + " is " + not + "a palindrome");
+
+        scanner.close();
+    }
+
+    // Returns true if the given string is a palindrome.
+    // Method ignores spaces and capitalization.
+    public static boolean palindrome(String s) {
+        s = s.toLowerCase().replaceAll(" ", "");
+
+        Stack<Character> stack = new Stack<>();
+
+        char[] letters = s.toCharArray();
+
+        for (int i = 0; i < s.length() / 2; i++) {
+            stack.push(letters[i]);
+        }
+
+        int plus = 0;
+
+        if (s.length() % 2 != 0) {
+            plus = 1;
+        }
+
+        for (int i = (s.length() / 2) + plus; i < s.length(); i++) {
+            if (stack.empty()) {
+                return true;
+            }
+
+            if (letters[i] == stack.peek()) {
+                stack.pop();
+            } else {
+                return false;
+            }
+        }
+
+        return true;
+    }
+}