CS/자료구조

2. 스택 (Stack)

SNNP 2020. 12. 30. 14:46

스택

- 데이터를 제한적으로 접근(한 쪽 끝에서만 자료를 넣거나 뺄 수 있음)

- 가장 나중에 쌓은 데이터를 가장 먼저 빼내는 데이터 구조

- 컴퓨터 내부 프로세스 구조의 함수 동작 방식

 

- push() : 데이터를 스택에 넣기

- pop() : 데이터를 스택에서 꺼내기

 

data_stack = list()

data_stack.append(1)
data_stack.append(2)

print(data_stack)
print(data_stack.pop())
[1, 2]
2

 

stack_list = list()

def push(data):
    stack_list.append(data)

def pop():
    data = stack_list[-1]   # -1 : 마지막 데이터
    del stack_list[-1]
    return data

for index in range(10) :
    push(index)

print(stack_list)
print(pop())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9