스택
- 영어로 Stack '쌓다'라는 의미
- 프로그래밍에서 목록 혹은 리스트에서 접근이 한 쪽에서만 가능한 구조
- LIFO(Last-In,First-Out)가 기본원리
- Python 스택 직접 구현
class Stack(list):
push=list.append
def peek(self):
return self[-1]
self[len(self)-1]
s=Stack()
s.push(1)
s.push(5)
s.push(10)
print("my stack is: ",s) #my stack is:[1,5,10]
print("popped value is: ", s.pop()) #popped value is : 10
print("my stack is: ",s) #my stack is:[1,5]
print("peeked value is: ", s.peek()) #popped value is : 5
s=[]
s.append(1)
s.append(5)
s.append(10)
print("my stack is: ",s) #my stack is:[1,5,10]
print("popped value is: ", s.pop()) #popped value is : 10
print("my stack is: ",s) #my stack is:[1,5]
print("peeked value is: ", s[-1]) #peeked value is : 5
print("my stack is: ",s) #my stack is:[1,5]
큐
- 영어로 Queue '일이 처리되기를 기다리는 리스트'라는 의미
- 프로그래밍에서 목록 혹은 리스트에서 접근이 양쪽에서 가능한 구조
- FIFO(First-In,First-Out)가 기본원리
- Python 큐 직접 구현
class Queue(list):
put=list.append
def peek(self):
return self[0]
q=Queue()
q.put(1)
q.put(5)
q.put(10)
print("my queue is: ",q) #my queue is:[1,5,10]
print("removed value is: ",q.get()) #removed value is:1
print("my queue is: ",q) #my queue is:[5,10]
print("peeked value is: ",q.peek()) #peeked value is:5
print("my queue is: ",q) #my queue is:[5,10]
from queue import Queue
q=Queue()
q.put(1)
q.put(5)
q.put(10)
print("my queue is: ",q) #my queue is: [1,5,10]
print("removed value is: ",q.get()) #removed value is:1
print("my queue is: ",q) #my queue is:[5,10]
print("peeked value is: ",q.peek()) #peeked value is:5
print("my queue is: ",q) #my queue is:[5,10]
q=[]
q.append(1)
q.append(5)
q.append(10)
print("my queue is: ",q) #my queue is:[1,5,10]
print("popped value is: ", q.pop(0)) #popped value is : 1
print("my stack is: ",s) #my stack is:[5,10]
print("peeked value is: ", q[0]) #peeked value is : 5
print("my queue is: ",q) #my queue is:[5,10]