반응형
- 노드생성
- 스택의 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())
'Algorithm' 카테고리의 다른 글
[1028알고리즘]Queue - JAVA (0) | 2021.04.10 |
---|---|
[1028알고리즘 12] 큐 구현 (0) | 2021.04.10 |
[1028알고리즘 10]스택 vs 큐 (0) | 2021.04.10 |
[1028알고리즘 09]링크드리스트 구현 (0) | 2021.04.10 |
[1028알고리즘 08]배열 vs 링크드리스트 (0) | 2021.04.10 |