데이터 구조 및 분석 ch_3_5 Stack
Jul 17, 2021
»
writing
KAIST 산업및시스템공학과 문일철_ 데이터 구조 및 분석 수업을 참고하여 작성하였습니다
ch_3_5 Stack
Stack
1. Structure of Stack
: linear like linked lists (A variation of a singly linked list)
: Voluntarily giving up
- Access to the middle in the linked list
- Only accesses to the first instance in the list
: The first instance in the list
= The top instacne in the stack
: is iserted or removed from the stack from one end called the 'top' of the stack
: is called Last-In-First-Out (LIFO)
2. Operation of Stack
- Push
= Insert an instance at the fist in the linked list
= Put an instance at the top in the stack
- Pop
= Remove and return an instance at the fist in the linked list
= Remove and return an instance at the top in the stack
3. Implementation of Stack
- Python code of a stack
- Utilizing a singly linked list
- To pop an instance (I retrieval count)
- To push an instnace (I retrieval count)
from edu.kaist.seslab.ie362.week3.SinglyLinkedList import SinglyLinkedList
class Stack(object) :
lstInstance = SinglyLinkedList()
def pop(self):
return self.lstInstance.removeAt(0)
def push(self):
self.lstInstance.insertAt(value, 0)
stack = Stack()
stack.push('a')
stack.push('b')
stack.push('c')
print(stack.pop())
print(stack.pop())
print(stack.pop())
# c
# b
# a
4. Balancing Symbols
- Algorithm for the balanced symbol checking
- Mack an empty stack
- read symbols until end of formula
- if 여는 symbol 있으면 push
- if 닫는 symbol 있으면, if stack is empty 면 error
- At the end of the of formula if the stack is not empty report an error