우주먼지
Stack을 이용한 오름차순 수열 구현
Data Architect/Algorithm 2023. 3. 7. 23:44

💡 오름차순 수열 구현 임의의 수열을 스택에 넣었다가 출력하는 방식으로 오름차순 수열을 출력할 수 있는지 확인. 출력할 수 있다면 push와 pop 연산을 어떤 순서로 수행해야 하는지 알아내는 프로그램 작성. push 연산은 +, pop 연산은 -, 불가능 할 때는 NO 출력. static int AA = 5; static int[] BB = {3,4,5,6,7}; public static void main(String[] args) { // average(A, B); stack(AA, BB); } // Stack의 오름차순 수열 구현 // 수열의 개수 a, 수열[] b public static String stack(int a, int[] b) { int[] A = new int[a]; for (int..

DFS(Recursion, Stack) & BFS(Queue) 구현
Data Architect/Algorithm 2023. 3. 7. 23:37

💡 DFS Depth-First Search의 약자이며 깊이 우선 탐색을 하는 알고리즘이다. 동작방식 스택을 이용하므로 재귀를 이용했을때 간결한 구현이 가능하며 시간복잡도는 O(N)이다. 탐색 시작 노드에 스택일 삽입하고 방문 처리한다. 스택의 최상단 노드에 방문하지 않은 인접 노드가 있으면 그 인접 노드를 스택에 넣어 방문 처리하고, 방문하지 않은 인접 노드가 없으면 스택에서 최상단 노드를 꺼낸다. 위의 과정을 더 이상 수행할 수 없을 때까지 반복한다. 방문처리란? 스택에 한번 삽입되어 처리된 노드가 다시 삽입되지 않게 체크하는 것을 의미한다. 이를 통해 각 노드를 한 번씩만 처리할 수 있다. 재귀 함수를 이용한 DFS 구현 노드들은 호출을 받으면 하위의 노드를 재귀 호출 하기 전, 자기 자신을 출력한..

Queue 구현
Data Architect/Algorithm 2023. 3. 7. 23:33

💡 ArrayList로 구현 public class Implementation_Queue { private ArrayList listQueue = new ArrayList(); public void add(Integer data) { listQueue.add(data); } public Integer poll() { if (listQueue.size() == 0) { return null; } else { return listQueue.remove(0); } } public int size() { return listQueue.size(); } public Integer peek() { if (listQueue.size() == 0) { return null; } else { return listQueu..

Stack 구현
Data Architect/Algorithm 2023. 3. 7. 23:30

💡 ArrayList로 스택 구현 public class Implementation_Stack { private ArrayList listStack = new ArrayList(); public void push(Integer data) { listStack.add(data); } public Integer pop() { if (listStack.size() == 0) { return null; } else { return listStack.remove(listStack.size()-1); } } public int size() { return listStack.size(); } public Integer peek() { if (listStack.size() == 0) { return null; } el..

검색 태그