| import java.util.NoSuchElementException; |
| |
| public class Queue { |
| |
| private static int MAX_QUE_SIZE = 10; |
| private int last; |
| private int first; |
| private int size; |
| private int[] data = new int[MAX_QUE_SIZE]; |
| |
| public Queue() { |
| |
| first = last = size = 0; |
| } |
| |
| public int size() { |
| |
| return size; |
| } |
| |
| public boolean isEmpty() { |
| return size==0; |
| } |
| |
| public void add(int item) { |
| last = (last+1) & data.length; |
| data[last] = item; |
| size++; |
| } |
| |
| public int pop() { |
| if (isEmpty()) { |
| throw new NoSuchElementException(); |
| } |
| |
| first = (first+1) % data.length; |
| int item = data[first]; |
| data[first] = 0; |
| size --; |
| |
| return item; |
| } |
| |
| public int peek() { |
| |
| if(isEmpty()) throw new NoSuchElementException(); |
| |
| return data[first]; |
| } |
| } |
| public class Stack { |
| |
| private static int MAX_STACK_SIZE = 10; |
| private int top; |
| private int[] data = new int[MAX_STACK_SIZE]; |
| |
| public Stack() { |
| top = -1; |
| } |
| |
| public void push(int data_) throws Exception { |
| |
| if(isFull()) { |
| throw new Exception("스택이 가득 찼습니다."); |
| } |
| data[++top] = data_; |
| } |
| |
| public int pop() throws Exception { |
| if(isEmpty()) { |
| throw new Exception("스택이 비었습니다."); |
| } |
| return data[top--]; |
| } |
| |
| public int peek() throws Exception { |
| if(isEmpty()) { |
| throw new Exception("스택이 비었습니다."); |
| } |
| return data[top]; |
| } |
| |
| public boolean isEmpty() { |
| return top ==-1; |
| } |
| |
| public boolean isFull() { |
| return top == MAX_STACK_SIZE-1; |
| } |
| |
| public int size() { |
| return top + 1; |
| } |
| } |
| import java.util.Stack; |
| |
| public class QueStack { |
| |
| Stack<Integer> oldStack; |
| Stack<Integer> newStack; |
| |
| public QueStack() { |
| oldStack = new Stack<>(); |
| newStack = new Stack<>(); |
| } |
| |
| public void enqueue(int a) { |
| oldStack.push(a); |
| } |
| |
| public int dequeue() { |
| int result = -1; |
| |
| if(newStack.isEmpty()) { |
| while(!oldStack.isEmpty()) { |
| newStack.push(oldStack.pop()); |
| } |
| |
| result = newStack.pop(); |
| } |
| |
| |
| if(!newStack.isEmpty()) { |
| while(!newStack.isEmpty()) { |
| oldStack.push(newStack.pop()); |
| } |
| } |
| |
| return result; |
| } |
| } |
| public class LinkedList<N> { |
| |
| private Node head; |
| private Node tail; |
| private int size = 0; |
| |
| private class Node{ |
| |
| private Object data; |
| |
| private Node next; |
| public Node(Object input) { |
| this.data = input; |
| this.next = null; |
| } |
| |
| public String toString(){ |
| return String.valueOf(this.data); |
| } |
| } |
| |
| public void addFirst(Object input){ |
| |
| Node newNode = new Node(input); |
| |
| newNode.next = head; |
| |
| head = newNode; |
| size++; |
| if(head.next == null){ |
| tail = head; |
| } |
| } |
| |
| public void addLast(Object input){ |
| |
| Node newNode = new Node(input); |
| |
| if(size == 0){ |
| addFirst(input); |
| } else { |
| |
| tail.next = newNode; |
| |
| tail = newNode; |
| |
| size++; |
| } |
| } |
| |
| Node node(int index) { |
| Node x = head; |
| for (int i = 0; i < index; i++) |
| x = x.next; |
| return x; |
| } |
| |
| public void add(int k, Object input){ |
| |
| if(k == 0){ |
| addFirst(input); |
| } else { |
| Node temp1 = node(k-1); |
| |
| Node temp2 = temp1.next; |
| |
| Node newNode = new Node(input); |
| |
| temp1.next = newNode; |
| |
| newNode.next = temp2; |
| size++; |
| |
| if(newNode.next == null){ |
| tail = newNode; |
| } |
| } |
| } |
| |
| public String toString() { |
| |
| if(head == null){ |
| return "[]"; |
| } |
| |
| Node temp = head; |
| String str = "["; |
| |
| |
| while(temp.next != null){ |
| str += temp.data + ","; |
| temp = temp.next; |
| } |
| |
| str += temp.data; |
| return str+"]"; |
| } |
| |
| public Object removeFirst(){ |
| |
| Node temp = head; |
| head = temp.next; |
| |
| Object returnData = temp.data; |
| temp = null; |
| size--; |
| return returnData; |
| } |
| |
| public Object remove(int k){ |
| if(k == 0) |
| return removeFirst(); |
| |
| Node temp = node(k-1); |
| |
| |
| Node todoDeleted = temp.next; |
| |
| temp.next = temp.next.next; |
| |
| Object returnData = todoDeleted.data; |
| if(todoDeleted == tail){ |
| tail = temp; |
| } |
| |
| todoDeleted = null; |
| size--; |
| return returnData; |
| } |
| |
| public Object removeLast(){ |
| return remove(size-1); |
| } |
| |
| public int size(){ |
| return size; |
| } |
| |
| public Object get(int k){ |
| Node temp = node(k); |
| return temp.data; |
| } |
| |
| public int indexOf(Object data){ |
| |
| Node temp = head; |
| |
| int index = 0; |
| |
| while(temp.data != data){ |
| temp = temp.next; |
| index++; |
| |
| if(temp == null) |
| return -1; |
| } |
| |
| return index; |
| } |
| } |
| public class HashTable { |
| LinkedList<Node>[] data; |
| |
| public HashTable(int size) { |
| this.data = new LinkedList[size]; |
| } |
| |
| int getHashCode(String key) { |
| int hashCode = 0; |
| for(char c : key.toCharArray()) { |
| hashCode += c; |
| } |
| |
| return hashCode; |
| } |
| |
| int convertToIndex(int hashCode) { |
| return hashCode % data.length; |
| } |
| |
| Node searchKey(LinkedList<Node> list, String key) { |
| if(list == null) return null; |
| |
| for(Node node : list) { |
| if(node.key.equals(key)) { |
| |
| return node; |
| } |
| } |
| |
| return null; |
| } |
| |
| void put(String key, String value) { |
| int hashCode = getHashCode(key); |
| int index = convertToIndex(hashCode); |
| |
| LinkedList<Node> list = data[index]; |
| |
| if(list == null) { |
| list = new LinkedList<Node>(); |
| data[index] = list; |
| } |
| |
| Node node = searchKey(list, key); |
| |
| if(node == null) { |
| list.addLast(new Node(key, value)); |
| } else { |
| node.setValue(value); |
| } |
| } |
| |
| public String get(String key) { |
| int hashCode = getHashCode(key); |
| int index = convertToIndex(hashCode); |
| LinkedList<Node> list = data[index]; |
| Node node = searchKey(list, key); |
| |
| return node == null ? "Not found" : node.getValue(); |
| } |
| |
| static class Node { |
| String key; |
| String value; |
| |
| public Node(String key, String value) { |
| this.key = key; |
| this.value = value; |
| } |
| |
| public String getValue() { |
| return this.value; |
| } |
| |
| public void setValue(String value) { |
| this.value = value; |
| } |
| } |
| } |
| class Singleton { |
| private Singleton() {} |
| |
| public static Singleton getInstance() { |
| return LazyHolder.INSTANCE; |
| } |
| |
| private static class LazyHolder { |
| private static final Singleton INSTANCE = new Singleton(); |
| } |
| } |
| public class Fibonacci { |
| static int[] cache = new int[11]; |
| public static void main(String[] args) { |
| System.out.println(loopFibonacci(8)); |
| System.out.println(recurFibonacci(8)); |
| System.out.println(dpFibonacci(8)); |
| } |
| |
| public static int loopFibonacci(int n) { |
| int a = 0, b = 1, c = 0; |
| if(n == 0) return 0; |
| else if(n == 1) return 1; |
| else { |
| for (int i = 2; i <= n; i++) { |
| c = a + b; |
| a = b; |
| b = c; |
| } |
| } |
| return c; |
| } |
| |
| |
| public static int recurFibonacci(int n) { |
| if(n == 0) return 0; |
| if(n == 1) return 1; |
| return recurFibonacci(n - 1) + recurFibonacci(n - 2); |
| } |
| |
| |
| public static int dpFibonacci(int n) { |
| if(n == 0) return 0; |
| if(n == 1) return 1; |
| int ret = cache[n]; |
| if(ret != 0) return ret; |
| return cache[n] = dpFibonacci(n - 1) + dpFibonacci(n - 2); |
| } |
| } |
| |
| |
| |
| |
| |
| static int K = 4; |
| static int ret = -1; |
| |
| public static void main(String[] args) { |
| int[] arr = {3, 5, 2, 4, 1}; |
| |
| QuickSelection(arr, 0, arr.length - 1); |
| System.out.println(ret); |
| } |
| |
| |
| public static void QuickSelection(int[] arr, int left, int right) { |
| int pivot = partition(arr, left, right); |
| |
| if(pivot == K - 1) { |
| ret = arr[pivot]; |
| } |
| else if(pivot > K - 1) { |
| QuickSelection(arr, left, pivot - 1); |
| } else { |
| QuickSelection(arr, pivot + 1, right); |
| } |
| } |
| |
| public static int partition(int[] arr, int left, int right) { |
| int mid = (left + right) / 2; |
| swap(arr, left, mid); |
| |
| int i = left, j = right; |
| int pivot = arr[left]; |
| |
| while (i < j) { |
| while(arr[j] >= pivot) |
| j--; |
| while(i < j && arr[i] <= pivot) |
| i++; |
| |
| swap(arr, i, j); |
| } |
| |
| arr[left] = arr[i]; |
| arr[i] = pivot; |
| |
| return i; |
| } |
| |
| public static void swap(int[] arr, int a, int b) { |
| int tmp = arr[a]; |
| arr[a] = arr[b]; |
| arr[b] = tmp; |
| } |
| import java.util.Random; |
| |
| public class RandomClass { |
| public static void main(String[] args) { |
| Random random = new Random(); |
| |
| for(int i = 0; i < 10; i++) { |
| System.out.println(random.nextInt(9) + 1); |
| } |
| } |
| } |
| |
| |
| import java.util.Arrays; |
| |
| public class Anagram { |
| public static void main(String[] args) { |
| char[] a = "acdbe".toCharArray(); |
| char[] b = "ecabd".toCharArray(); |
| Arrays.sort(a); |
| Arrays.sort(b); |
| |
| if(Arrays.equals(a, b)) |
| System.out.println(true); |
| else |
| System.out.println(false); |
| } |
| } |
| |
| |
| |
| public class GCD_LCM { |
| public static void main(String[] args) { |
| System.out.println(gcd(8, 12)); |
| |
| int LCM = (8 * 12) / gcd(8, 12); |
| System.out.println(LCM); |
| } |
| |
| |
| public static int gcd(int a, int b) { |
| if(b == 0) return a; |
| return gcd(b, a % b); |
| } |
| } |
| public class TreeTraversal { |
| class Node { |
| int data; |
| Node left, right; |
| |
| public Node(int data) { |
| this.data = data; |
| } |
| } |
| |
| class Tree { |
| public Node root; |
| |
| public void setRoot(Node node) { |
| root = node; |
| } |
| |
| public Node getRoot() { |
| return root; |
| } |
| |
| public Node createNode(int data, Node left, Node right) { |
| Node node = new Node(data); |
| node.left = left; |
| node.right = right; |
| return node; |
| } |
| |
| public void inOrder(Node node) { |
| if(node == null) return; |
| |
| inOrder(node.left); |
| System.out.println(node.data); |
| inOrder(node.right); |
| } |
| |
| public void preOrder(Node node) { |
| if(node == null) return; |
| |
| System.out.println(node.data); |
| preOrder(node.left); |
| preOrder(node.right); |
| } |
| |
| public void postOrder(Node node) { |
| if(node == null) return; |
| |
| postOrder(node.left); |
| postOrder(node.right); |
| System.out.println(node.data); |
| } |
| } |
| } |