알고리즘 5일차

알고리즘 공부 마지막날이다. 이번 일주일동안 알고리즘에 대해 개념도 쌓을 수 있었고 어느정도 자신감도 쌓을 수 있는 아주아주 바람직한 한 주 였지만, 오늘 하루만 본다면 굉장히 실망스럽다. 하루 종일 공부도 잘 안되고 집중도 잘 안됬다. 기분이 태도가 되지 않게 항상 노력해야 하지만 정말 어려운거 같다.


오늘 한 일

  • 알고리즘 스택, 큐 정리!
  • 백준 문제 풀이 및 해설

✔Stack

자료를 배열에 넣었을 때, 가장 먼저 들어 간것이 가장 나중에 나오는 자료 구조를 스택이라고 한다.(First In Last Out) 이러한 자료 구조는 우리가 일상속에서 볼 수 있는 ctrl + z 기능에서 잘 볼 수 있다. 링크드 리스트의 형태로 구현된 코드를 알아보자.

  • 코드
 
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class Stack:
    def __init__(self):
        self.head = None

    def push(self, value):
        new_node = Node(value)
        new_node.next = self.head
        self.head = new_node
        return

    # pop 기능 구현
    def pop(self):
        if self.is_empty():
            return "Stack은 비어 있습니다."
        delete_node = self.head
        self.head = self.head.next
        return delete_node.data

    def peek(self):
        if self.is_empty():
            return "Stack은 비어 있습니다."
        return self.head.data

    # isEmpty 기능 구현
    def is_empty(self):
        return self.head is None


stack = Stack()

stack.push(1)
print(stack.is_empty())
stack.push(2)
print(stack.pop())
print(stack.peek())
print(stack.pop())
print(stack.peek())
print(stack.pop())
print(stack.is_empty())

# result
# False
# 2
# 1
# 1
# Stack은 비어 있습니다.
# Stack은 비어 있습니다.
# True

        

✔Queue

자료를 배열에 넣을 때, 가장 먼저 들어간 것을 가장 먼저 빼는 자료 구조를 스택이라고 한다.(First In First Out) 이러한 자료 구조에서는 스택에서 head만을 가지고 있는 것과 다르게 head, tail을 갖고 있다. 새로운 자료는 tail로 들어가고 원하는 자료는 head에서 꺼내 사용하는 형태를 갖는다. 링크드 리스트의 형태로 구현된 코드를 알아보자.

  • 코드

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


class Queue:
    def __init__(self):
        self.head = None
        self.tail = None

    def enqueue(self, value):
        new_node = Node(value)
        if self.is_empty():
            self.head = new_node
            self.tail = new_node
        self.tail.next = new_node
        self.tail = new_node
        return

    def dequeue(self):
        if self.is_empty():
            return "비어있어!"
        delete_node = self.head
        self.head = self.head.next
        return delete_node.data

    def peek(self):
        if self.is_empty():
            return "비어있어"
        return self.head.data

    def is_empty(self):
        return self.head is None


f1 = Queue()

f1.enqueue(3)
f1.enqueue(4)
f1.enqueue(5)
f1.enqueue(6)
print(f1.peek())
print(f1.dequeue())
print(f1.dequeue())
print(f1.dequeue())
print(f1.is_empty())
print(f1.dequeue())
print(f1.dequeue())
print(f1.is_empty())
print(f1.peek())

#result
# 3
# 3
# 4
# 5
# False
# 6
# 비어있어!
# True
# 비어있어


✔생각 정리

알고리즘 주간이 끝나고 내일부터는 새로운 기술을 배울 차례다 자바를 사용하다고 했는데 나는 아직 자바가 너무 어려워서 너무 걱정이다. 내일 부터는 자바 위주로 학습을 이어나가야겠다 또한, 알고리즘 공부는 지속적으로 이어나가야겠다. 지금의 흐름을 놓치고 싶지는 않다.

  • 정규 시간에는 캠프 일정 따라가기
  • 나만의 시간에 JAVA 언어 공부하기
  • 백준 알고리즘 단계별 문제 풀기
profile
잘 부탁드려요

0개의 댓글