mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 05:33:40 +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:
152
ko/codes/csharp/chapter_stack_and_queue/array_deque.cs
Normal file
152
ko/codes/csharp/chapter_stack_and_queue/array_deque.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* File: array_deque.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 원형 배열로 구현한 덱 */
|
||||
public class ArrayDeque {
|
||||
int[] nums; // 덱의 원소를 저장하는 배열
|
||||
int front; // 맨 앞 원소를 가리키는 맨 앞 포인터
|
||||
int queSize; // 덱 길이
|
||||
|
||||
/* 생성자 */
|
||||
public ArrayDeque(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 덱의 용량 반환 */
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 원형 배열 인덱스 계산 */
|
||||
int Index(int i) {
|
||||
// 나머지 연산을 통해 배열의 처음과 끝을 연결한다
|
||||
// i가 배열의 끝을 넘어가면 처음으로 돌아온다
|
||||
// i가 배열의 처음을 넘어가면 끝으로 돌아온다
|
||||
return (i + Capacity()) % Capacity();
|
||||
}
|
||||
|
||||
/* 맨 앞에 삽입 */
|
||||
public void PushFirst(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("덱이 가득 찼다");
|
||||
return;
|
||||
}
|
||||
// 맨 앞 포인터를 왼쪽으로 한 칸 이동한다
|
||||
// 나머지 연산을 통해 front가 배열의 처음을 넘어가면 끝으로 돌아오게 한다
|
||||
front = Index(front - 1);
|
||||
// num을 맨 앞에 추가한다
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 맨 뒤에 삽입 */
|
||||
public void PushLast(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("덱이 가득 찼다");
|
||||
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 InvalidOperationException();
|
||||
}
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 맨 뒤 원소 접근 */
|
||||
public int PeekLast() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
// 마지막 원소의 인덱스를 계산한다
|
||||
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 {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 덱 초기화 */
|
||||
ArrayDeque deque = new(10);
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("덱 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 원소 접근 */
|
||||
int peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("맨 앞 원소 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.PeekLast();
|
||||
Console.WriteLine("맨 뒤 원소 peekLast = " + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("원소 4를 맨 뒤에 삽입한 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("원소 1을 맨 앞에 삽입한 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int popLast = deque.PopLast();
|
||||
Console.WriteLine("맨 뒤에서 꺼낸 원소 = " + popLast + ", 맨 뒤에서 꺼낸 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
int popFirst = deque.PopFirst();
|
||||
Console.WriteLine("맨 앞에서 꺼낸 원소 = " + popFirst + ", 맨 앞에서 꺼낸 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("덱 길이 size = " + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("덱이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
114
ko/codes/csharp/chapter_stack_and_queue/array_queue.cs
Normal file
114
ko/codes/csharp/chapter_stack_and_queue/array_queue.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* File: array_queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 원형 배열로 구현한 큐 */
|
||||
class ArrayQueue {
|
||||
int[] nums; // 큐의 원소를 저장하는 배열
|
||||
int front; // 맨 앞 원소를 가리키는 맨 앞 포인터
|
||||
int queSize; // 큐 길이
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 큐의 용량 반환 */
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 큐에 삽입 */
|
||||
public void Push(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("큐가 가득 찼다");
|
||||
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 Exception();
|
||||
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 % this.Capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 큐 초기화 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new(capacity);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("큐 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("맨 앞 원소 peek = " + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("큐에서 꺼낸 원소 pop = " + pop + ", 큐에서 꺼낸 후 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("큐 길이 size = " + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("큐가 비어 있는가 = " + isEmpty);
|
||||
|
||||
/* 원형 배열 테스트 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.Push(i);
|
||||
queue.Pop();
|
||||
Console.WriteLine("반복 " + i + " 회에서 삽입 + 꺼내기를 수행한 후 queue = " + string.Join(",", queue.ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
84
ko/codes/csharp/chapter_stack_and_queue/array_stack.cs
Normal file
84
ko/codes/csharp/chapter_stack_and_queue/array_stack.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* File: array_stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 배열로 구현한 스택 */
|
||||
class ArrayStack {
|
||||
List<int> stack;
|
||||
public ArrayStack() {
|
||||
// 리스트(동적 배열)를 초기화한다
|
||||
stack = [];
|
||||
}
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
public int Size() {
|
||||
return stack.Count;
|
||||
}
|
||||
|
||||
/* 스택이 비어 있는지 판단 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 스택에 삽입 */
|
||||
public void Push(int num) {
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* 스택에서 꺼내기 */
|
||||
public int Pop() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
var val = Peek();
|
||||
stack.RemoveAt(Size() - 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stack[Size() - 1];
|
||||
}
|
||||
|
||||
/* List를 Array로 변환하여 반환 */
|
||||
public int[] ToArray() {
|
||||
return [.. stack];
|
||||
}
|
||||
}
|
||||
|
||||
public class array_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 스택 초기화 */
|
||||
ArrayStack stack = new();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("스택 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("스택 맨 위 원소 peek = " + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("스택에서 꺼낸 원소 pop = " + pop + ", 스택에서 꺼낸 후 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("스택의 길이 size = " + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("스택이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
44
ko/codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
44
ko/codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.cs
|
||||
* Created Time: 2022-12-30
|
||||
* Author: moonache (microin1301@outlook.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 덱 초기화 */
|
||||
// C#에서는 연결 리스트 LinkedList를 덱으로 사용한다
|
||||
LinkedList<int> deque = new();
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.AddLast(2); // 맨 뒤에 추가한다
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // 맨 앞에 추가한다
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("덱 deque = " + string.Join(",", deque));
|
||||
|
||||
/* 원소 접근 */
|
||||
int? peekFirst = deque.First?.Value; // 맨 앞 원소
|
||||
Console.WriteLine("맨 앞 원소 peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.Last?.Value; // 맨 뒤 원소
|
||||
Console.WriteLine("맨 뒤 원소 peekLast = " + peekLast);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
deque.RemoveFirst(); // 맨 앞 원소를 꺼낸다
|
||||
Console.WriteLine("맨 앞 원소를 꺼낸 후 deque = " + string.Join(",", deque));
|
||||
deque.RemoveLast(); // 맨 뒤 원소를 꺼낸다
|
||||
Console.WriteLine("맨 뒤 원소를 꺼낸 후 deque = " + string.Join(",", deque));
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("덱 길이 size = " + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("덱이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
179
ko/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Normal file
179
ko/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* File: linkedlist_deque.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 이중 연결 리스트 노드 */
|
||||
public class ListNode(int val) {
|
||||
public int val = val; // 노드 값
|
||||
public ListNode? next = null; // 다음 노드 참조
|
||||
public ListNode? prev = null; // 이전 노드 참조
|
||||
}
|
||||
|
||||
/* 이중 연결 리스트로 구현한 덱 */
|
||||
public class LinkedListDeque {
|
||||
ListNode? front, rear; // 머리 노드 front, 꼬리 노드 rear
|
||||
int queSize = 0; // 덱의 길이
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 큐 삽입 연산 */
|
||||
void Push(int num, bool isFront) {
|
||||
ListNode node = new(num);
|
||||
// 연결 리스트가 비어 있으면 front와 rear가 모두 node를 가리키게 한다
|
||||
if (IsEmpty()) {
|
||||
front = node;
|
||||
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);
|
||||
}
|
||||
|
||||
/* 큐에서 꺼내는 연산 */
|
||||
int? Pop(bool isFront) {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
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 Exception();
|
||||
return front?.val;
|
||||
}
|
||||
|
||||
/* 맨 뒤 원소 접근 */
|
||||
public int? PeekLast() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
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 {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 덱 초기화 */
|
||||
LinkedListDeque deque = new();
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("덱 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 원소 접근 */
|
||||
int? peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("맨 앞 원소 peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.PeekLast();
|
||||
Console.WriteLine("맨 뒤 원소 peekLast = " + peekLast);
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("원소 4를 맨 뒤에 삽입한 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("원소 1을 맨 앞에 삽입한 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int? popLast = deque.PopLast();
|
||||
Console.WriteLine("맨 뒤에서 꺼낸 원소 = " + popLast + ", 맨 뒤에서 꺼낸 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
int? popFirst = deque.PopFirst();
|
||||
Console.WriteLine("맨 앞에서 꺼낸 원소 = " + popFirst + ", 맨 앞에서 꺼낸 후 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 덱의 길이 반환 */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("덱 길이 size = " + size);
|
||||
|
||||
/* 덱이 비어 있는지 판단 */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("덱이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
106
ko/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Normal file
106
ko/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: linkedlist_queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 연결 리스트로 구현한 큐 */
|
||||
class LinkedListQueue {
|
||||
ListNode? front, rear; // 머리 노드 front, 꼬리 노드 rear
|
||||
int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 큐에 삽입 */
|
||||
public void Push(int num) {
|
||||
// 꼬리 노드 뒤에 num을 추가한다
|
||||
ListNode node = new(num);
|
||||
// 큐가 비어 있으면 머리와 꼬리 노드가 모두 해당 노드를 가리키게 한다
|
||||
if (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// 큐가 비어 있지 않으면 해당 노드를 꼬리 노드 뒤에 추가한다
|
||||
} else if (rear != null) {
|
||||
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 Exception();
|
||||
return front!.val;
|
||||
}
|
||||
|
||||
/* 연결 리스트를 Array로 변환하여 반환 */
|
||||
public int[] ToArray() {
|
||||
if (front == null)
|
||||
return [];
|
||||
|
||||
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 {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 큐 초기화 */
|
||||
LinkedListQueue queue = new();
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("큐 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("맨 앞 원소 peek = " + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("큐에서 꺼낸 원소 pop = " + pop + ", 큐에서 꺼낸 후 queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("큐 길이 size = " + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("큐가 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
97
ko/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Normal file
97
ko/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* File: linkedlist_stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 연결 리스트로 구현한 스택 */
|
||||
class LinkedListStack {
|
||||
ListNode? stackPeek; // 머리 노드를 스택의 맨 위로 사용한다
|
||||
int stkSize = 0; // 스택의 길이
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
public int Size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* 스택이 비어 있는지 판단 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* 스택에 삽입 */
|
||||
public void Push(int num) {
|
||||
ListNode node = new(num) {
|
||||
next = stackPeek
|
||||
};
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* 스택에서 꺼내기 */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
stackPeek = stackPeek!.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek!.val;
|
||||
}
|
||||
|
||||
/* List를 Array로 변환하여 반환 */
|
||||
public int[] ToArray() {
|
||||
if (stackPeek == null)
|
||||
return [];
|
||||
|
||||
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 {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 스택 초기화 */
|
||||
LinkedListStack stack = new();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("스택 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("스택 맨 위 원소 peek = " + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("스택에서 꺼낸 원소 pop = " + pop + ", 스택에서 꺼낸 후 stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("스택의 길이 size = " + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("스택이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
39
ko/codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
39
ko/codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 큐 초기화 */
|
||||
Queue<int> queue = new();
|
||||
|
||||
/* 큐에 원소 삽입 */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(5);
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("큐 queue = " + string.Join(",", queue));
|
||||
|
||||
/* 맨 앞 원소 접근 */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("맨 앞 원소 peek = " + peek);
|
||||
|
||||
/* 큐에서 원소 꺼내기 */
|
||||
int pop = queue.Dequeue();
|
||||
Console.WriteLine("큐에서 꺼낸 원소 pop = " + pop + ", 큐에서 꺼낸 후 queue = " + string.Join(",", queue));
|
||||
|
||||
/* 큐의 길이 반환 */
|
||||
int size = queue.Count;
|
||||
Console.WriteLine("큐 길이 size = " + size);
|
||||
|
||||
/* 큐가 비어 있는지 판단 */
|
||||
bool isEmpty = queue.Count == 0;
|
||||
Console.WriteLine("큐가 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
40
ko/codes/csharp/chapter_stack_and_queue/stack.cs
Normal file
40
ko/codes/csharp/chapter_stack_and_queue/stack.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 스택 초기화 */
|
||||
Stack<int> stack = new();
|
||||
|
||||
/* 스택에 원소 삽입 */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
// stack.ToArray()는 역순 수열을 반환하며, 인덱스 0이 스택의 맨 위이다
|
||||
Console.WriteLine("스택 stack = " + string.Join(",", stack));
|
||||
|
||||
/* 스택 맨 위 원소 접근 */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("스택 맨 위 원소 peek = " + peek);
|
||||
|
||||
/* 스택에서 원소 꺼내기 */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("스택에서 꺼낸 원소 pop = " + pop + ", 스택에서 꺼낸 후 stack = " + string.Join(",", stack));
|
||||
|
||||
/* 스택의 길이 반환 */
|
||||
int size = stack.Count;
|
||||
Console.WriteLine("스택의 길이 size = " + size);
|
||||
|
||||
/* 비어 있는지 판단 */
|
||||
bool isEmpty = stack.Count == 0;
|
||||
Console.WriteLine("스택이 비어 있는가 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user