알고리즘 기본기 다지기3.기초 데이터구조 (스택, 큐)
스택(Stack)
호출 함수들이나 웹페이지들의 목록 등과 같은 저장소의 특징은 가장 최근에 들어온 것이 가장 먼저 빠져나간다는 점과 그러한 행위가 맨 위라는 특정 위치에서만 이루어진다는 것이다.
이러한 추상화된 데이터구조를 스택(Stack)이라고 한다.
스택은 임의의 객체를 후입선출(LIFO, Last In First Out) 순서로 삽입/삭제한다.
주요 메소드
push(e) : 원소 e를 삽입
element pop() : 가장 최근에 삽인된 원소를 삭제하여 반환
스택 구현
스택은 순차 데이터구조인 배열과 리스트를 사용하여 구현할 수 있다.
배열
top의 변수를 생성하여 삽입/삭제에 따른 인덱스를 관리한다.
기억장ㅇ소 사용은 원소수와 동일한 O(n)이며, 실행시간은 O(1)이 된다.
스택의 크기는 선언시 정해져야한다. 설계한 스택의 크기가 사용량에 비해 작을 경우 push 시도시 구현상 오류가 발생한다.
리스트
배열 구조대신, 단일 연결 리스트를 사용하여 스택을 구현하면 연결스택(linked stack)이라고 한다. 삽입/삽제가 top에서만 이루어짐으로 헤더노드가 필수요소는 아니다.
기억 장소는 원소 수 만큼 노드를 사용해야 하므로 O(n)이며, 스택의 각 작업시간은 O(1)이다.
문제풀이
많은 코딩테스트를 진행하진 않았지만, 스택/큐는 한문제쯤 꼭 나오는 것 같다.
특히 스택이 나오면 대부분 괄호 문제였다.
백준 10828번. 스택
정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
package stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
정수를 저장하는 스택을 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
명령은 총 다섯 가지이다.
push X: 정수 X를 스택에 넣는 연산이다.
pop: 스택에서 가장 위에 있는 정수를 빼고, 그 수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
size: 스택에 들어있는 정수의 개수를 출력한다.
empty: 스택이 비어있으면 1, 아니면 0을 출력한다.
top: 스택의 가장 위에 있는 정수를 출력한다. 만약 스택에 들어있는 정수가 없는 경우에는 -1을 출력한다.
* */
public class BackJun10828 {
private static int commandCount;
private static List<Integer> stack = new ArrayList<Integer>();
public static void setCommandCount(int cmdCnt) {
commandCount = cmdCnt;
}
public static void execute(String str) {
String[] strArr = str.split(" ");
String cmd = strArr[0];
String num = strArr.length > 1 ? strArr[1]: null;
if(cmd.equals("push")) {
push(Integer.valueOf(num));
}
else if(cmd.equals("pop")) {
System.out.println(pop());
}
else if(cmd.equals("size")) {
System.out.println(size());
}
else if(cmd.equals("empty")) {
System.out.println(empty());
}
else if(cmd.equals("top")) {
System.out.println(top());
}
}
public static void push(int e) {
stack.add(e);
}
public static int pop() {
int top = top();
if(empty() == 0) {
stack.remove(size()-1);
}
return top;
}
public static int size() {
return stack.size();
}
public static int empty() {
int empty = 1;
if(size() > 0) {
empty = 0;
}
return empty;
}
public static int top() {
int top = -1;
if(empty() == 0) {
top = stack.get(size()-1);
}
return top;
}
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String str = br.readLine();
setCommandCount(Integer.valueOf(str));
for(int i=0;i<commandCount ; i++) {
execute(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test() {
setCommandCount(14);
execute("push 1");
execute("push 2");
execute("top");
execute("size");
execute("empty");
execute("pop");
execute("pop");
execute("pop");
execute("size");
execute("empty");
execute("pop");
execute("push 3");
execute("empty");
execute("top");
}
}
백준 9012번. 괄호
package stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import org.junit.jupiter.api.Test;
/**
* 괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그
* 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고 부른다. 한 쌍의 괄호 기호로 된 “(
* )” 문자열은 기본 VPS 이라고 부른다. 만일 x 가 VPS 라면 이것을 하나의 괄호에 넣은 새로운 문자열 “(x)”도 VPS 가 된다.
* 그리고 두 VPS x 와 y를 접합(concatenation)시킨 새로운 문자열 xy도 VPS 가 된다. 예를 들어 “(())()”와
* “((()))” 는 VPS 이지만 “(()(”, “(())()))” , 그리고 “(()” 는 모두 VPS 가 아닌 문자열이다.
*
* 여러분은 입력으로 주어진 괄호 문자열이 VPS 인지 아닌지를 판단해서 그 결과를 YES 와 NO 로 나타내어야 한다.
*/
public class Backjun9012 {
private static int cnt=0;
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String str = br.readLine();
setCnt(Integer.valueOf(str));
for(int i=0;i<cnt ; i++) {
checkVps(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
//입력 횟수 저장
public static void setCnt(int count) {
cnt = count;
}
//올바른 괄호 문자열 확인
public static void checkVps(String ps) {
Stack<Character> stack = new Stack<Character>();
for(char ch : ps.toCharArray()) {
if(stack.isEmpty()) {
stack.push(ch);
}
else {
if(stack.size() == 1 && stack.peek() == ')') {
break;
}
if(stack.peek() == ch) {
stack.push(ch);
}
else {
stack.pop();
}
}
}
if(stack.isEmpty()) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
@Test
public void tests() {
String str = "6";
setCnt(Integer.valueOf(str));
checkVps("(())())");
checkVps("(((()())()");
checkVps("(()())((()))");
checkVps("((()()(()))(((())))()");
checkVps("()()()()(()()())()");
checkVps("(()((())()(");
System.out.println("---------------------");
str = "3";
setCnt(Integer.valueOf(str));
checkVps("((");
checkVps("))");
checkVps("())(()");
}
}
백준 10773번. 제로
package stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
import org.junit.jupiter.api.Test;
/**
* 나코더 기장 재민이는 동아리 회식을 준비하기 위해서 장부를 관리하는 중이다.
*
* 재현이는 재민이를 도와서 돈을 관리하는 중인데, 애석하게도 항상 정신없는 재현이는 돈을 실수로 잘못 부르는 사고를 치기 일쑤였다.
*
* 재현이는 잘못된 수를 부를 때마다 0을 외쳐서, 가장 최근에 재민이가 쓴 수를 지우게 시킨다.
*
* 재민이는 이렇게 모든 수를 받아 적은 후 그 수의 합을 알고 싶어 한다. 재민이를 도와주자!
*/
public class Backjun10773 {
private static int cnt = 0;
private static Stack<Integer> stack = new Stack<Integer>();
@Test
public void tests() {
setCnt(4);
execute(3);
execute(0);
execute(4);
execute(0);
sumOfStacks();
}
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String str = br.readLine();
setCnt(Integer.valueOf(str));
for(int i=0;i<cnt ; i++) {
execute(Integer.valueOf(br.readLine()));
}
sumOfStacks();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setCnt(int count) {
cnt = count;
}
public static void execute(int num) {
if(num == 0 && !stack.isEmpty()) {
stack.pop();
}
else {
stack.add(num);
}
}
public static void sumOfStacks() {
int sum = stack.stream().mapToInt(i->i).sum();
System.out.println(sum);
}
}
큐(Queue)
큐(Queue)는 대기열을 추상화한 데이터 구조이다. 큐에서 들어오고 빠져 나가는 삽입과 삭제는 선입선출(FIFO, First In First Out)의 순서를 따른다. 삽입은 큐의 rear, 삭제는 큐의 front라 불리는 위치에서 이루어진다.
주요 메소드
enqueue(e) : 큐의 rear에 원소 e를 삽입
element dequeue() : 큐의 front에서 원소를 삭제하여 반환
구현
큐는 스택과 같이 배열 또는 연결리스트로 구현할 수 있다.
이를 선형으로 구현할 수 있으며, 원형으로 사용하여 구현할 수 있다.
스택에서는 top변수 하나를 이용하여 위치를 기억하였지만, 큐에서는 front, rear 두개의 변수를 이용하여 위치를 기억한다.
문제풀이
백준 10845번. 큐
package queue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.jupiter.api.Test;
/**
* 정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
*
* 명령은 총 여섯 가지이다.
*
* push X: 정수 X를 큐에 넣는 연산이다. pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다.
* 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
* size: 큐에 들어있는 정수의 개수를 출력한다.
* empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
* front: 큐의 가장 앞에 있는 정수를 출력한다.
* 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
* back: 큐의 가장 뒤에 있는 정수를 출력한다.
* 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
*/
public class Backjun10845 {
//1.배열로 원형 큐를 만들어보자.
private static int[] queue;
private static int size;
private static int back = -1; // = rear
private static int front = -1;
private static int queueSize = 0;
@Test
public void test() {
setSize(10000);
execute("push 20000");
for( int i=0;i<size/3;i++) {
execute("push 100000");
execute("pop");
execute("empty");
}
}
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String str = br.readLine();
setSize(Integer.valueOf(str));
//명령어 수만큼 큐에 대한 명령을 실행한다.
for(int i=0;i<size ; i++) {
execute(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
//2. 명령의 수/큐의 사이즈를 저장한다.
public static void setSize(int sz) {
size = sz;
//주어지는 사이즈보다 큐의 사이즈가 커질 수 없다.
//사이즈를 큐의 데이터로 하자
queue = new int[sz+1];
}
//3. 명령을 실행한다.
public static void execute(String str) {
//공백으로 명령을 나눈다.
String[] arr = str.split(" ");
String command = arr[0];
int x = arr.length>1 ? Integer.valueOf(arr[1]):0;
if(command.equals("push")) {
push(x);
}
else if(command.equals("front")) {
if(empty()==0) {
System.out.println(queue[front]);
}
else {
System.out.println(-1);
}
}
else if(command.equals("back")) {
if(empty()==0) {
System.out.println(queue[back]);
}
else {
System.out.println(-1);
}
}
else if(command.equals("size")) {
System.out.println(size());
}
else if(command.equals("empty")) {
System.out.println(empty());
}
else if(command.equals("pop")) {
System.out.println(pop());
}
}
public static void push(int x) {
if(size() == queue.length) {
//full
return;
}
else if(empty() == 1) {
queue[++back] = x;
front++;
}
else {
queue[++back] = x;
}
queueSize++;
}
public static int pop() {
int v = -1;
if(empty() == 1) {
return -1;
}
else if(front == back) {
v = queue[front];
back = -1;
front = -1;
queueSize=0;
}
else {
v = queue[front++];
queueSize--;
}
return v;
}
public static int size() {
return queueSize;
}
public static int empty() {
if(queueSize == 0) {
return 1;
}
return 0;
}
}
참조
https://ko.m.wikipedia.org/wiki/자료_구조
자료 구조 - 위키백과, 우리 모두의 백과사전
자료구조(資料構造, 영어: data structure)는 컴퓨터 과학에서 효율적인 접근 및 수정을 가능케 하는 자료의 조직, 관리, 저장을 의미한다.[1][2][3] 더 정확히 말해, 자료 구조는 데이터 값의 모임, 또
ko.m.wikipedia.org
알고리즘 국형준 저
https://github.com/cri-kim/BlogPractice/tree/main/computer-science/data-structure
데이터 구조 풀이