ID_DI
DI's study notes
ID_DI
전체 방문자
오늘
어제
  • 분류 전체보기 (85)
    • Deep-Learning (3)
      • CNN (2)
      • NLP (1)
    • Data_Python (6)
      • Numpy (0)
      • Matplotlib (4)
    • Python (8)
      • Python Algorithm (6)
    • Java (36)
      • Java(base) (33)
      • Java practice(base) (2)
    • Git (12)
    • Algorithm (7)
    • etc (7)
    • linux (1)
    • DeskSetup (0)
    • TIL_모각코 (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 파이썬
  • staged
  • git add.
  • string to int
  • matplotlib
  • README.md
  • git
  • 정렬
  • java
  • 합병정렬
  • 자바
  • Python
  • binarySearch
  • java base
  • java.net
  • 커밋
  • Github
  • 알고리즘
  • java.lang
  • java 기초

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ID_DI

DI's study notes

[python](자료구조) Queue class 구현
Python/Python Algorithm

[python](자료구조) Queue class 구현

2021. 8. 25. 17:41

Queue 클래스 구현

  • 중복된 값 enqueue 불가능
  • Stack의 size 값이 defuat 일 경우 5로 설정
  • overflow / underflow 주의

  • 생성자
    • queue 리스트 생성
    • size 초기화
    • rear, front 초기화
  • enqueue() dequeue() view() 메소드 구현
class Queue:
    def __init__(self, size = 5):
        self.queue = [] #큐를 리스트로 구현
        self.size = size #큐의 사이즈
        self.front = 0 #큐의 앞쪽 포인터
        self.rear = 0 #큐의 뒤쪽 포인터

    def enqueue(self, data):
        if data in self.queue: #중복된 값 방지
            print("{} 값은 이미 큐에 있는 데이터입니다.".format(data))
        else:
            if self.size <= self.rear: #큐의 사이즈가 rear 보다 작으면 overflow 
                print("overflow...큐가 가득찼습니다.")
            else: 
                self.queue.append(data)
                self.rear += 1 #추가했을 때, rear 크기 증가
                print("{} 값을 추가했습니다.".format(data))
            self.view()

    def dequeue(self):
        if self.size <= 0 or self.rear == self.front:
            print("dequeue() 를 실행할 값이 없습니다.")
        else:
            print("{} 값을 추출했습니다.".format(self.queue[0]))
            del self.queue[0]
            self.front += 1 #추출했을 때, front 값 증가
        self.view()

    def view(self):
        print("큐의 데이터 값: ", end ='')
        if self.size <= 0 or self.rear == self.front:
            print("없음")
        else:
            for elem in self.queue:
                print(elem, end = " ")
            print()

실행

queue = Queue()
queue.view()
queue.enqueue(111)
queue.enqueue(111)  # 중복 데이터
queue.enqueue(999)
queue.enqueue(333)
queue.enqueue(888)
queue.enqueue(555)
queue.enqueue(777)  # overflow
queue.dequeue() #추출
print('=' * 80)
queue.view()
print('=' * 80)

결과

큐의 데이터 값: 없음
111 값을 추가했습니다.
큐의 데이터 값: 111 
111 값은 이미 큐에 있는 데이터입니다.
999 값을 추가했습니다.
큐의 데이터 값: 111 999 
333 값을 추가했습니다.
큐의 데이터 값: 111 999 333 
888 값을 추가했습니다.
큐의 데이터 값: 111 999 333 888
555 값을 추가했습니다.
큐의 데이터 값: 111 999 333 888 555
overflow...큐가 가득찼습니다.
큐의 데이터 값: 111 999 333 888 555
111 값을 추출했습니다.
큐의 데이터 값: 999 333 888 555
================================================================================
큐의 데이터 값: 999 333 888 555
================================================================================

'Python > Python Algorithm' 카테고리의 다른 글

[python](알고리즘) quickSort 퀵정렬(랜덤 피봇) 구현  (0) 2021.10.06
[python](알고리즘) mergeSort 병합정렬(합병정렬) 구현  (0) 2021.10.01
[python] factorial 함수, 재귀함수 구현  (0) 2021.08.27
[python] (알고리즘) binarySearch (이진탐색) 구현  (0) 2021.08.27
[python](자료구조) stack class 구현  (0) 2021.08.25
    'Python/Python Algorithm' 카테고리의 다른 글
    • [python](알고리즘) mergeSort 병합정렬(합병정렬) 구현
    • [python] factorial 함수, 재귀함수 구현
    • [python] (알고리즘) binarySearch (이진탐색) 구현
    • [python](자료구조) stack class 구현
    ID_DI
    ID_DI
    Computer Vision

    티스토리툴바