PS 3

큐 구현(python)

우선 리스트를 큐처럼 활용해보았다.# 1. 리스트를 큐처럼 활용하기 queue = []queue.append(1)queue.append(2)queue.append(3)first_item = queue.pop(0) # 인덱스 0번 출력print(first_item) # 출력 : 1queue.append(4)queue.append(5)first_item = queue.pop(0)print(first_item) # 출력 : 2 다음으론 시간복잡도를 낮추기 위하여 파이썬의 collections모듈의 데크를 가져와 큐를 구현해보았다.#2. 덱을 큐처럼 활용하기 - 시간복잡도 낮음from collections import dequeque = deque() que.append(1)que.append(2)que.ap..

PS 2025.08.16

연결리스트 구현(python)

파이썬으로 연결리스트를 구현해보았다class Node: def __init__(self, data): self.data = data self.next = None# 대원칙 : 삽입위치의 뒤를 먼저 연결하고, 그다음 삽입위치의 앞을 연결한다.# 연결 리스트 클래스 만들기class LinkedList: def __init__(self): self.head = None #공백 리스트 생성 self.length = 0 def __len__(self): # 길이 계산 메서드 return self.length def appendleft(self, data): #왼쪽에다 노드삽입 if self.head is Non..

PS 2025.08.16