Algorithm

[1028알고리즘 11] 스택구현

  • 노드생성
  • 스택의 push _입력
  • 스택의 pop _ 빼내기._head
  • 스택의 peek_맨앞데이터 보기
  • 스택의 isEmpty() 스택이 비어있는지 여부.
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


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

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

    def pop(self):
        return_node = self.head
        if self.is_empty():
            print("비었음")
            return
        self.head = return_node.next
        return return_node

    def peek(self):
        if self.is_empty():
            print("비었음")
            return
        return self.head.data

    def isEmpty(self):
        if self.head is None:
            return True
        return False


s = Stack()
s.push("b")
s.push("c")
print(s.peek())
print(s.pop().data)
print(s.peek())
print(s.isEmpty())
print(s.pop().data)
print(s.isEmpty())