반응형
Notice
Recent Posts
Recent Comments
Link
7JeY world
[자료구조 / Python] 원형 큐, 원형 덱 | Circular Queue, Circular Deque using Python 본문
Programming/Data Structure
[자료구조 / Python] 원형 큐, 원형 덱 | Circular Queue, Circular Deque using Python
7JeY 2021. 4. 11. 15:58반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | MAX_QSIZE = 10 class CircularQueue : def __init__(self): self.front = 0 self.rear = 0 self.items = [None] * MAX_QSIZE def isEmpty(self): return self.front == self.rear def isFull(self): return self.front == (self.rear+1)%MAX_QSIZE def clear(self): self.front = self.rear def __len__(self): return (self.rear - self.front + MAX_QSIZE) % MAX_QSIZE def enqueue(self, item): if not self.isFull(): self.rear = (self.rear + 1) % MAX_QSIZE self.items[self.rear] = item def dequeue(self): if not self.isEmpty(): self.front = (self.front+1) % MAX_QSIZE return self.items[self.front] def peek(self): if not self.isEmpty(): return self.items[(self.front+1)%MAX_QSIZE] def print(self): out = [] if self.front < self.rear: out = self.items[self.front+1:self.rear+1] else: out = self.items[self.front+1:MAX_QSIZE] + self.items[0:self.rear+1] print("[f=%s, r=%d] ==> "%(self.front, self.rear), out) class CircularDeque(CircularQueue): def __init__(self): super().__init__() def addRear (self, item): self.enqueue(item) def deleteFront (self): return self.dequeue() def getFront(self): return self.peek() def addFront(self, item): if not self.isFull(): self.items[self.front] = item self.front = (self.front - 1 + MAX_QSIZE) % MAX_QSIZE def deleteRear(self): if not self.isEmpty(): item = self.items[self.rear] self.rear = (self.rear - 1 + MAX_QSIZE) % MAX_QSIZE return item def getRear(self): return self.items[self.rear] | cs |
반응형
'Programming > Data Structure' 카테고리의 다른 글
[자료구조 / Python] Stack (0) | 2021.04.11 |
---|---|
[자료구조 / Python] 원형리스트 | Circular Linked List using Python (0) | 2021.04.11 |
[자료구조 / Python] 이중 연결 리스트 | Doubly Linked List using Python (0) | 2021.04.11 |
[자료구조 / Python] 단일 연결 리스트 | Singly Linked List using Python (0) | 2021.04.11 |
[자료구조 / Java] 이중 연결 리스트를 이용한 덱(Deque) (0) | 2020.10.04 |
Comments