Stack에 대해서 알아보자
* 데이터를 제한적으로 접근할 수 있는 구조
* 한쪽끝에서만 데이터를 넣고 뺄수 있는 구조
* 가장 나중에 쌓은 데이터가 제일 먼저 나가는 구조
프로세스에서 많이 쓰임
1. 스택 구조
스택은 LIFO(Last In, First Out)구조
대표적인 함수 : push,(데이터 넣기), pop(데이터 빼기)
Visualgo site에서 push, pop 기능으로 확인해보자
VisuAlgo - Linked List (Single, Doubly), Stack, Queue, Deque
VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter
visualgo.net
스택의 장점
구조가 단순해서 구현이 쉽다.
데이터 저장/읽기 속도가 빠르다.
스택의 단점
미리 크기를 알아야한다.
불필요한 메모리 낭비가 있을 수 있다. (미리 데이터 크기 지정하고, 그만큼 안쓰면 낭비..)
python에서는 재귀 함수를 1000번까지만 호출가능하도록 시스템으로 구현되어 있음
python에서 스택 구현(array 이용)
* append(push), pop
data_stack = list()
data_stack.append(1)
data_stack.append(2)
print(data_stack.pop())
print(data_stack.pop())
stack_list = list()
def push(data):
stack_list.append(data)
def pop():
data = stack_list[-1]
stack_list.pop()
return data
for index in range(10):
push(index)
pop()