자료구조와 알고리즘 실습 | Data Structures & Algorithms Lab
스택의 기본 개념: 데이터를 push합니다
stack = [None, None, None, None, None]
top = -1
top += 1
stack[top] = "커피"
top += 1
stack[top] = "녹차"
top += 1
stack[top] = "꿀물"
print("----- 스택 상태 -----")
for i in range(len(stack)-1, -1, -1) :
print(stack[i])
Stack basics: push data onto a stack
stack = [None, None, None, None, None]
top = -1
top += 1
stack[top] = "Coffee"
top += 1
stack[top] = "GreenTea"
top += 1
stack[top] = "HoneyWater"
print("----- Stack Status -----")
for i in range(len(stack)-1, -1, -1) :
print(stack[i])
스택의 기본 개념: 데이터를 pop합니다
stack = ["커피", "녹차", "꿀물", None, None]
top = 2
print("--- 스택 상태-----")
for i in range(len(stack)-1, -1, -1) :
print(stack[i])
print("-------------------")
data = stack[top]
stack[top] = None
top -= 1
print("pop -->", data)
data = stack[top]
stack[top] = None
top -= 1
print("pop -->", data)
data = stack[top]
stack[top] = None
top -= 1
print("pop -->", data)
print("-------------------")
print("--- 스택 상태-----")
for i in range(len(stack)-1, -1, -1) :
print(stack[i])
Stack basics: pop data from a stack
stack = ["Coffee", "GreenTea", "HoneyWater", None, None]
top = 2
print("--- Stack Status ---")
for i in range(len(stack)-1, -1, -1) :
print(stack[i])
print("-------------------")
data = stack[top]
stack[top] = None
top -= 1
print("pop -->", data)
data = stack[top]
stack[top] = None
top -= 1
print("pop -->", data)
data = stack[top]
stack[top] = None
top -= 1
print("pop -->", data)
print("-------------------")
print("--- Stack Status ---")
for i in range(len(stack)-1, -1, -1) :
print(stack[i])
스택 Full 상태를 확인하는 함수를 구현합니다
def isStackFull() :
SIZE = 5
stack = ["커피", "녹차", "꿀물", "콜라", "환타"]
top = 4
print("스택이 꽉 찼는지 여부 ==>", isStackFull())
Implements a function to check if the stack is full
def isStackFull() :
SIZE = 5
stack = ["Coffee", "GreenTea", "HoneyWater", "Cola", "Fanta"]
top = 4
print("Whether the stack is full ==>", isStackFull())
스택 Full 확인 후 데이터를 push합니다
def isStackFull() :
def push(data) :
SIZE = 5
stack = ["커피", "녹차", "꿀물", "콜라", None]
top = 3
print(stack)
push("환타")
print(stack)
push("게토레이")
Pushes data after checking if the stack is full
def isStackFull() :
def push(data) :
SIZE = 5
stack = ["Coffee", "GreenTea", "HoneyWater", "Cola", None]
top = 3
print(stack)
push("Fanta")
print(stack)
push("Gatorade")
스택 Empty 상태를 확인하는 함수를 구현합니다
def isStackEmpty() :
SIZE = 5
stack = [ None for _ in range(SIZE) ]
top = -1
print("스택이 비었는지 여부 ==>", isStackEmpty())
Implements a function to check if the stack is empty
def isStackEmpty() :
SIZE = 5
stack = [ None for _ in range(SIZE) ]
top = -1
print("Whether the stack is empty ==>", isStackEmpty())
스택 Empty 확인 후 데이터를 pop합니다
def isStackEmpty() :
def pop() :
SIZE = 5
stack = ["커피", None, None, None, None]
top = 0
print(stack)
retData = pop()
print("추출한 데이터 -->", retData)
print(stack)
retData = pop()
Pops data after checking if the stack is empty
def isStackEmpty() :
def pop() :
SIZE = 5
stack = ["Coffee", None, None, None, None]
top = 0
print(stack)
retData = pop()
print("Extracted data -->", retData)
print(stack)
retData = pop()
스택의 top 데이터를 확인하는 peek 함수를 구현합니다
def isStackEmpty() :
def peek() :
SIZE = 5
stack = ["커피", "녹차", "꿀물", None, None]
top = 2
print(stack)
retData = peek()
print("top의 데이터 확인 -->", retData)
print(stack)
Implements a peek function to check the top of the stack
def isStackEmpty() :
def peek() :
SIZE = 5
stack = ["Coffee", "GreenTea", "HoneyWater", None, None]
top = 2
print(stack)
retData = peek()
print("Check top data -->", retData)
print(stack)
스택의 push/pop/peek 통합 프로그램입니다
## 함수 선언 부분 ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## 전역 변수 선언 부분 ##
SIZE = int(input("스택의 크기를 입력하세요 ==> "))
stack = [ None for _ in range(SIZE) ]
top = -1
## 메인 코드 부분 ##
Complete stack program with push, pop, and peek
## Function Declarations ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## Global Variables ##
SIZE = int(input("Enter the stack size ==> "))
stack = [ None for _ in range(SIZE) ]
top = -1
## Main Code ##
if __name__ == "__main__" :
select = input("Choose: Insert(I)/Extract(E)/Check(V)/Quit(X) ==> ")
while (select != 'X' and select != 'x') :
if select=='I' or select =='i' :
data = input("Data to input ==> ")
push(data)
print("Stack status: ", stack)
elif select=='E' or select =='e' :
data = pop()
print("Extracted data ==> ", data)
print("Stack status: ", stack)
elif select=='V' or select =='v' :
data = peek()
print("Checked data ==> ", data)
print("Stack status: ", stack)
else :
print("Invalid input")
select = input("Choose: Insert(I)/Extract(E)/Check(V)/Quit(X) ==> ")
print("Program terminated!")
스택을 활용한 웹 브라우저 뒤로가기를 구현합니다
import webbrowser import time ## 함수 선언 부분 ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## 전역 변수 선언 부분 ## SIZE = 100 stack = [ None for _ in range(SIZE) ] top = -1 ## 메인 코드 부분 ##
Implements browser back-navigation using a stack
import webbrowser import time ## Function Declarations ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## Global Variables ##
SIZE = 100
stack = [ None for _ in range(SIZE) ]
top = -1
## Main Code ##
if __name__ == "__main__" :
urls = [ 'naver.com', 'daum.net', 'nate.com']
for url in urls :
push(url)
webbrowser.open('http://'+url)
print(url, end = '-->')
time.sleep(1)
print("Traversal complete")
time.sleep(5)
while True :
url = pop()
if url == None :
break
webbrowser.open('http://'+url)
print(url, end = '-->')
time.sleep(1)
print("Traversal complete")
스택을 활용한 괄호 검사를 구현합니다
## 함수 선언 부분 ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
def checkBracket(expr) :
## 전역 변수 선언 부분 ## SIZE = 100 stack = [ None for _ in range(SIZE) ] top = -1 ## 메인 코드 부분 ##
Implements parenthesis validation using a stack
## Function Declarations ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
def checkBracket(expr) :
## Global Variables ##
SIZE = 100
stack = [ None for _ in range(SIZE) ]
top = -1
## Main Code ##
if __name__ == "__main__" :
exprAry = ['(A+B)', ')A+B(', '((A+B)-C', '(A+B]', '(<A+{B-C}/[C*D]>)']
for expr in exprAry :
top = -1
print(expr, '==>', checkBracket(expr))
스택 응용: 랜덤 데이터로 push/pop 실습
import random ## 함수 선언 부분 ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## 전역 변수 선언 부분 ## SIZE = 10 stack = [ None for _ in range(SIZE) ] top = -1 ## 메인 코드 부분 ##
Stack application: push/pop practice with random data
import random ## Function Declarations ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## Global Variables ##
SIZE = 10
stack = [ None for _ in range(SIZE) ]
top = -1
## Main Code ##
if __name__ == "__main__" :
stoneAry = ["Red", "Blue", "Green", "Yellow", "Purple", "Orange"]
random.shuffle(stoneAry)
print("Path to snack shop : ", end = ' ')
for stone in stoneAry :
push(stone)
print(stone, "-->", end = ' ')
print("SnackShop")
print("Path to my house : ", end = ' ')
while True :
stone = pop()
if stone == None :
break
print(stone, "-->", end = ' ')
print("MyHouse")
스택 응용: 수식의 괄호 짝 검사
## 함수 선언 부분 ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## 전역 변수 선언 부분 ## SIZE = 100 stack = [ None for _ in range(SIZE) ] top = -1 ## 메인 코드 부분 ##
Stack application: expression bracket matching
## Function Declarations ## def isStackFull() :
def isStackEmpty() :
def push(data) :
def pop() :
def peek() :
## Global Variables ##
SIZE = 100
stack = [ None for _ in range(SIZE) ]
top = -1
## Main Code ##
if __name__ == "__main__" :
with open("Azaleas.txt", 'r', encoding='UTF8') as rfp :
lineAry = rfp.readlines()
print("----- Original -----")
for line in lineAry :
push(line)
print(line, end = ' ')
print()
print("----- Result processed in reverse -----")
while True :
line = pop()
if line == None :
break
miniStack = [None for _ in range(len(line))]
miniTop = -1
for ch in line :
miniTop += 1
miniStack[miniTop] = ch
while True :
if miniTop == -1 :
break
ch = miniStack[miniTop]
miniTop -= 1
print(ch, end = ' ')
push 함수를 직접 구현합니다
def push(data) :
SIZE = 5
stack = ["커피", "녹차", "꿀물", "콜라", None]
top = 3
print(stack)
push("환타")
print(stack)
push("게토레이")Implements the push function yourself
def push(data) :
SIZE = 5
stack = ["Coffee", "GreenTea", "HoneyWater", "Cola", None]
top = 3
print(stack)
push("Fanta")
print(stack)
push("Gatorade")pop 함수를 직접 구현합니다
def pop() :
SIZE = 5
stack = ["커피", None, None, None, None]
top = 0
print(stack)
retData = pop()
print("추출한 데이터 -->", retData)
print(stack)
retData = pop()Implements the pop function yourself
def pop() :
SIZE = 5
stack = ["Coffee", None, None, None, None]
top = 0
print(stack)
retData = pop()
print("Extracted data -->", retData)
print(stack)
retData = pop()