mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-01 18:55:23 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
/**
|
||||
* File: array_deque.java
|
||||
* Created Time: 2023-02-16
|
||||
* Author: krahets ([email protected]), FangYuan33 ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 원형 배열로 구현한 덱 */
|
||||
class ArrayDeque {
|
||||
private int[] nums; // 덱의 원소를 저장하는 배열
|
||||
private int front; // 맨 앞 원소를 가리키는 맨 앞 포인터
|
||||
private int queSize; // 덱 길이
|
||||
|
||||
/* 생성자 */
|
||||
public ArrayDeque(int capacity) {
|
||||
this.nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 덱의 용량 반환 */
|
||||
public int capacity() {
|
||||
return nums.length;
|
||||
}
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 원형 배열 인덱스 계산 */
|
||||
private int index(int i) {
|
||||
// 나머지 연산을 통해 배열의 처음과 끝을 연결한다
|
||||
// i가 배열의 끝을 넘어가면 처음으로 돌아온다
|
||||
// i가 배열의 처음을 넘어가면 끝으로 돌아온다
|
||||
return (i + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* 맨 앞에 삽입 */
|
||||
public void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("덱이 가득 찼다");
|
||||
return;
|
||||
}
|
||||
// 맨 앞 포인터를 왼쪽으로 한 칸 이동한다
|
||||
// 나머지 연산을 통해 front가 배열의 처음을 넘어가면 끝으로 돌아오게 한다
|
||||
front = index(front - 1);
|
||||
// num을 맨 앞에 추가한다
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 삽입 */
|
||||
public void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("덱이 가득 찼다");
|
||||
return;
|
||||
}
|
||||
// 맨 뒤 인덱스 + 1을 가리키는 맨 뒤 포인터를 계산한다
|
||||
int rear = index(front + queSize);
|
||||
// num을 맨 뒤에 추가한다
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 맨 앞에서 꺼내기 */
|
||||
public int popFirst() {
|
||||
int num = peekFirst();
|
||||
// 맨 앞 포인터를 뒤로 한 칸 이동한다
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 뒤에서 꺼내기 */
|
||||
public int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 맨 뒤 원소 접근 */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
// 마지막 원소의 인덱스를 계산한다
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* 출력에 사용할 배열 반환 */
|
||||
public int[] toArray() {
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_deque {
|
||||
public static void main(String[] args) {
|
||||
/* 덱 초기화 */
|
||||
ArrayDeque deque = new ArrayDeque(10);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.println("덱 deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* 원소 접근 */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("맨 앞 원소 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("맨 뒤 원소 peekLast = " + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.pushLast(4);
|
||||
System.out.println("원소 4를 맨 뒤에 삽입한 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("원소 1을 맨 앞에 삽입한 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("맨 뒤에서 꺼낸 원소 = " + popLast + ", 맨 뒤에서 꺼낸 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("맨 앞에서 꺼낸 원소 = " + popFirst + ", 맨 앞에서 꺼낸 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
int size = deque.size();
|
||||
System.out.println("덱 길이 size = " + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("덱이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* File: array_queue.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 원형 배열로 구현한 큐 */
|
||||
class ArrayQueue {
|
||||
private int[] nums; // 큐의 원소를 저장하는 배열
|
||||
private int front; // 맨 앞 원소를 가리키는 맨 앞 포인터
|
||||
private int queSize; // 큐 길이
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 큐의 용량 반환 */
|
||||
public int capacity() {
|
||||
return nums.length;
|
||||
}
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
public boolean isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 큐에 삽입 */
|
||||
public void push(int num) {
|
||||
if (queSize == capacity()) {
|
||||
System.out.println("큐가 가득 찼다");
|
||||
return;
|
||||
}
|
||||
// 맨 뒤 인덱스 + 1을 가리키는 맨 뒤 포인터를 계산한다
|
||||
// 나머지 연산을 통해 rear가 배열의 끝을 넘어가면 처음으로 돌아오게 한다
|
||||
int rear = (front + queSize) % capacity();
|
||||
// num을 맨 뒤에 추가한다
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 큐에서 꺼내기 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// 맨 앞 포인터를 뒤로 한 칸 이동하고, 끝을 넘어가면 배열의 처음으로 돌아온다
|
||||
front = (front + 1) % capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 배열 반환 */
|
||||
public int[] toArray() {
|
||||
// 유효한 길이 범위 안의 리스트 원소만 변환한다
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue {
|
||||
public static void main(String[] args) {
|
||||
/* 큐 초기화 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
System.out.println("큐 queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
int peek = queue.peek();
|
||||
System.out.println("맨 앞 원소 peek = " + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int pop = queue.pop();
|
||||
System.out.println("큐에서 꺼낸 원소 pop = " + pop + ", 큐에서 꺼낸 후 queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
int size = queue.size();
|
||||
System.out.println("큐 길이 size = " + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("큐가 비어 있는가 = " + isEmpty);
|
||||
|
||||
/* 원형 배열 테스트 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
System.out.println("반복 " + i + " 회에서 삽입 + 꺼내기를 수행한 후 queue = " + Arrays.toString(queue.toArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* File: array_stack.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 배열로 구현한 스택 */
|
||||
class ArrayStack {
|
||||
private ArrayList<Integer> stack;
|
||||
|
||||
public ArrayStack() {
|
||||
// 리스트(동적 배열)를 초기화한다
|
||||
stack = new ArrayList<>();
|
||||
}
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
public int size() {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* 스택이 비어 있는지 판단 */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 스택에 삽입 */
|
||||
public void push(int num) {
|
||||
stack.add(num);
|
||||
}
|
||||
|
||||
/* 스택에서 꺼내기 */
|
||||
public int pop() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.remove(size() - 1);
|
||||
}
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
/* List를 Array로 변환하여 반환 */
|
||||
public Object[] toArray() {
|
||||
return stack.toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public class array_stack {
|
||||
public static void main(String[] args) {
|
||||
/* 스택 초기화 */
|
||||
ArrayStack stack = new ArrayStack();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
System.out.println("스택 stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
int peek = stack.peek();
|
||||
System.out.println("스택 맨 위 원소 peek = " + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
int pop = stack.pop();
|
||||
System.out.println("스택에서 꺼낸 원소 pop = " + pop + ", 스택에서 꺼낸 후 stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
int size = stack.size();
|
||||
System.out.println("스택의 길이 size = " + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("스택이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: deque.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class deque {
|
||||
public static void main(String[] args) {
|
||||
/* 덱 초기화 */
|
||||
Deque<Integer> deque = new LinkedList<>();
|
||||
deque.offerLast(3);
|
||||
deque.offerLast(2);
|
||||
deque.offerLast(5);
|
||||
System.out.println("덱 deque = " + deque);
|
||||
|
||||
/* 원소 접근 */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("맨 앞 원소 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("맨 뒤 원소 peekLast = " + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.offerLast(4);
|
||||
System.out.println("원소 4를 맨 뒤에 삽입한 후 deque = " + deque);
|
||||
deque.offerFirst(1);
|
||||
System.out.println("원소 1을 맨 앞에 삽입한 후 deque = " + deque);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int popLast = deque.pollLast();
|
||||
System.out.println("맨 뒤에서 꺼낸 원소 = " + popLast + ", 맨 뒤에서 꺼낸 후 deque = " + deque);
|
||||
int popFirst = deque.pollFirst();
|
||||
System.out.println("맨 앞에서 꺼낸 원소 = " + popFirst + ", 맨 앞에서 꺼낸 후 deque = " + deque);
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
int size = deque.size();
|
||||
System.out.println("덱 길이 size = " + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("덱이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* File: linkedlist_deque.java
|
||||
* Created Time: 2023-01-20
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 이중 연결 리스트 노드 */
|
||||
class ListNode {
|
||||
int val; // 노드 값
|
||||
ListNode next; // 다음 노드 참조
|
||||
ListNode prev; // 이전 노드 참조
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
prev = next = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* 이중 연결 리스트로 구현한 덱 */
|
||||
class LinkedListDeque {
|
||||
private ListNode front, rear; // 머리 노드 front, 꼬리 노드 rear
|
||||
private int queSize = 0; // 덱의 길이
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = rear = null;
|
||||
}
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 큐 삽입 연산 */
|
||||
private void push(int num, boolean isFront) {
|
||||
ListNode node = new ListNode(num);
|
||||
// 연결 리스트가 비어 있으면 front와 rear가 모두 node를 가리키게 한다
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// 맨 앞에 삽입하는 연산
|
||||
else if (isFront) {
|
||||
// node를 연결 리스트의 맨 앞에 추가한다
|
||||
front.prev = node;
|
||||
node.next = front;
|
||||
front = node; // 머리 노드를 갱신한다
|
||||
// 맨 뒤에 삽입하는 연산
|
||||
} else {
|
||||
// node를 연결 리스트의 맨 뒤에 추가한다
|
||||
rear.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // 꼬리 노드를 갱신한다
|
||||
}
|
||||
queSize++; // 큐 길이를 갱신한다
|
||||
}
|
||||
|
||||
/* 맨 앞에 삽입 */
|
||||
public void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* 맨 뒤에 삽입 */
|
||||
public void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
|
||||
/* 큐에서 꺼내는 연산 */
|
||||
private int pop(boolean isFront) {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
int val;
|
||||
// 큐의 길이가 1이면 머리와 꼬리가 같은 노드를 가리킨다
|
||||
if (queSize == 1) {
|
||||
val = front.val;
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
// 맨 앞에서 꺼내는 연산
|
||||
else if (isFront) {
|
||||
val = front.val; // 머리 노드의 값을 임시로 저장한다
|
||||
// 머리 노드를 삭제한다
|
||||
ListNode fNext = front.next;
|
||||
fNext.prev = null;
|
||||
front.next = null;
|
||||
front = fNext; // 머리 노드를 갱신한다
|
||||
// 맨 뒤에서 꺼내는 연산
|
||||
} else {
|
||||
val = rear.val; // 꼬리 노드의 값을 임시로 저장한다
|
||||
// 꼬리 노드를 삭제한다
|
||||
ListNode rPrev = rear.prev;
|
||||
rPrev.next = null;
|
||||
rear.prev = null;
|
||||
rear = rPrev; // 꼬리 노드를 갱신한다
|
||||
}
|
||||
queSize--; // 큐 길이를 갱신한다
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 맨 앞에서 꺼내기 */
|
||||
public int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* 맨 뒤에서 꺼내기 */
|
||||
public int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
public int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 맨 뒤 원소 접근 */
|
||||
public int peekLast() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return rear.val;
|
||||
}
|
||||
|
||||
/* 출력에 사용할 배열 반환 */
|
||||
public int[] toArray() {
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
for (int i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_deque {
|
||||
public static void main(String[] args) {
|
||||
/* 덱 초기화 */
|
||||
LinkedListDeque deque = new LinkedListDeque();
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
System.out.println("덱 deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* 원소 접근 */
|
||||
int peekFirst = deque.peekFirst();
|
||||
System.out.println("맨 앞 원소 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.peekLast();
|
||||
System.out.println("맨 뒤 원소 peekLast = " + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.pushLast(4);
|
||||
System.out.println("원소 4를 맨 뒤에 삽입한 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
deque.pushFirst(1);
|
||||
System.out.println("원소 1을 맨 앞에 삽입한 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int popLast = deque.popLast();
|
||||
System.out.println("맨 뒤에서 꺼낸 원소 = " + popLast + ", 맨 뒤에서 꺼낸 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
int popFirst = deque.popFirst();
|
||||
System.out.println("맨 앞에서 꺼낸 원소 = " + popFirst + ", 맨 앞에서 꺼낸 후 deque = " + Arrays.toString(deque.toArray()));
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
int size = deque.size();
|
||||
System.out.println("덱 길이 size = " + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
System.out.println("덱이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* File: linkedlist_queue.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* 연결 리스트로 구현한 큐 */
|
||||
class LinkedListQueue {
|
||||
private ListNode front, rear; // 머리 노드 front, 꼬리 노드 rear
|
||||
private int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
public int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 큐에 삽입 */
|
||||
public void push(int num) {
|
||||
// 꼬리 노드 뒤에 num을 추가한다
|
||||
ListNode node = new ListNode(num);
|
||||
// 큐가 비어 있으면 머리와 꼬리 노드가 모두 해당 노드를 가리키게 한다
|
||||
if (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// 큐가 비어 있지 않으면 해당 노드를 꼬리 노드 뒤에 추가한다
|
||||
} else {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 큐에서 꺼내기 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
// 머리 노드를 삭제한다
|
||||
front = front.next;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return front.val;
|
||||
}
|
||||
|
||||
/* 연결 리스트를 Array로 변환하여 반환 */
|
||||
public int[] toArray() {
|
||||
ListNode node = front;
|
||||
int[] res = new int[size()];
|
||||
for (int i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_queue {
|
||||
public static void main(String[] args) {
|
||||
/* 큐 초기화 */
|
||||
LinkedListQueue queue = new LinkedListQueue();
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
System.out.println("큐 queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
int peek = queue.peek();
|
||||
System.out.println("맨 앞 원소 peek = " + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int pop = queue.pop();
|
||||
System.out.println("큐에서 꺼낸 원소 pop = " + pop + ", 큐에서 꺼낸 후 queue = " + Arrays.toString(queue.toArray()));
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
int size = queue.size();
|
||||
System.out.println("큐 길이 size = " + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("큐가 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* File: linkedlist_stack.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
/* 연결 리스트로 구현한 스택 */
|
||||
class LinkedListStack {
|
||||
private ListNode stackPeek; // 머리 노드를 스택의 맨 위로 사용한다
|
||||
private int stkSize = 0; // 스택의 길이
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
public int size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* 스택이 비어 있는지 판단 */
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 스택에 삽입 */
|
||||
public void push(int num) {
|
||||
ListNode node = new ListNode(num);
|
||||
node.next = stackPeek;
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* 스택에서 꺼내기 */
|
||||
public int pop() {
|
||||
int num = peek();
|
||||
stackPeek = stackPeek.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
public int peek() {
|
||||
if (isEmpty())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stackPeek.val;
|
||||
}
|
||||
|
||||
/* List를 Array로 변환하여 반환 */
|
||||
public int[] toArray() {
|
||||
ListNode node = stackPeek;
|
||||
int[] res = new int[size()];
|
||||
for (int i = res.length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_stack {
|
||||
public static void main(String[] args) {
|
||||
/* 스택 초기화 */
|
||||
LinkedListStack stack = new LinkedListStack();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
System.out.println("스택 stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
int peek = stack.peek();
|
||||
System.out.println("스택 맨 위 원소 peek = " + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
int pop = stack.pop();
|
||||
System.out.println("스택에서 꺼낸 원소 pop = " + pop + ", 스택에서 꺼낸 후 stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
int size = stack.size();
|
||||
System.out.println("스택의 길이 size = " + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("스택이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: queue.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class queue {
|
||||
public static void main(String[] args) {
|
||||
/* 큐 초기화 */
|
||||
Queue<Integer> queue = new LinkedList<>();
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.offer(1);
|
||||
queue.offer(3);
|
||||
queue.offer(2);
|
||||
queue.offer(5);
|
||||
queue.offer(4);
|
||||
System.out.println("큐 queue = " + queue);
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
int peek = queue.peek();
|
||||
System.out.println("맨 앞 원소 peek = " + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int pop = queue.poll();
|
||||
System.out.println("큐에서 꺼낸 원소 pop = " + pop + ", 큐에서 꺼낸 후 queue = " + queue);
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
int size = queue.size();
|
||||
System.out.println("큐 길이 size = " + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
boolean isEmpty = queue.isEmpty();
|
||||
System.out.println("큐가 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: stack.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets ([email protected])
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class stack {
|
||||
public static void main(String[] args) {
|
||||
/* 스택 초기화 */
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
System.out.println("스택 stack = " + stack);
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
int peek = stack.peek();
|
||||
System.out.println("스택 맨 위 원소 peek = " + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
int pop = stack.pop();
|
||||
System.out.println("스택에서 꺼낸 원소 pop = " + pop + ", 스택에서 꺼낸 후 stack = " + stack);
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
int size = stack.size();
|
||||
System.out.println("스택의 길이 size = " + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
boolean isEmpty = stack.isEmpty();
|
||||
System.out.println("스택이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user