-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.py
More file actions
53 lines (43 loc) · 1.06 KB
/
stack.py
File metadata and controls
53 lines (43 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Python list 사용
class Stack_list(list): # 리스트를 상속받는다.
push = list.append # Insert
# pop # Delete - 내장 pop 메소드 활용
def is_empty(self):
if not self:
return True
else:
return False
def peek(self):
return self[-1]
# Python Node 사용
class Node:
def __init__(self, data):
self.data = data
self.next = None
class Stack: # head만으로 구현이 가능하다.
def __init__(self):
self.head = None
def is_empty(self):
if not self.head:
return True
return False
def push(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def pop(self):
if self.is_empty():
return None
ret_data = self.head.data
self.head = self.head.next
return ret_data
def peek(self):
if self.is_empty():
return None
return self.head.data
# Python
# 내장된 메소드
# List: pop
# https://docs.python.org/ko/3/tutorial/datastructures.html
# Stack: __init__ -> head
# Node: __init__ -> data, next