유물/알고리즘

<자료구조> 스택

디벅잉 2022. 3. 15. 00:08
728x90

 

🎯

 

스택

1. 데이터를 추가하면 맨 마지막 위치에 자리합니다.

2. 삭제를 하면 맨 마지막 위치의 데이터를 삭제합니다(삭제 데이터 반환).

 

구현 (파이썬)

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


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

    def push(self, item):
        self.top = Node(item, self.top)

    def pop(self):
        if self.top is None:
            return None

        node = self.top
        self.top = self.top.next

        return node.item

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

 

728x90