Skip to content

Commit 0137d2f

Browse files
committed
queue
1 parent efc00ff commit 0137d2f

File tree

5 files changed

+133
-13
lines changed

5 files changed

+133
-13
lines changed

7장_추상_데이터_구조/1_stack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __repr__(self):
3131
if __name__ == '__main__':
3232
stack = Stack()
3333
print(f"스택이 비었나요? {stack.isEmpty()}")
34-
print("스택에 0-9까지의 항목을 추가합니다.")
34+
print("스택에 숫자 0~9를 추가합니다.")
3535
for i in range(10):
3636
stack.push(i)
3737
print(f"스택 크기: {stack.size()}")

7장_추상_데이터_구조/2_stack_with_node.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,34 @@ def pop(self):
2020
self.head = node.pointer
2121
return node.value
2222
else:
23-
print('Stack is empty.')
23+
print("Stack is empty.")
2424

2525
def peek(self):
2626
if self.head:
2727
return self.head.value
2828
else:
29-
print('Stack is empty.')
29+
print("Stack is empty.")
3030

3131
def size(self):
3232
node = self.head
3333
count = 0
3434
while node:
35-
count +=1
35+
count += 1
3636
node = node.pointer
3737
return count
3838

3939
def _printList(self):
4040
node = self.head
4141
while node:
42-
print(node.value, end=' ')
42+
print(node.value, end=" ")
4343
node = node.pointer
4444
print()
4545

4646

4747
if __name__ == '__main__':
4848
stack = Stack()
4949
print(f"스택이 비었나요? {stack.isEmpty()}")
50-
print("스택에 0-9까지의 항목을 추가합니다.")
50+
print("스택에 숫자 0~9를 추가합니다.")
5151
for i in range(10):
5252
stack.push(i)
5353
stack._printList()

7장_추상_데이터_구조/3_queue.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def __repr__(self):
2323

2424
if __name__ == '__main__':
2525
queue = Queue()
26-
print("Is the queue empty? ", queue.isEmpty())
27-
print("Adding 0 to 10 in the queue...")
26+
print(f"큐가 비었나요? {queue.isEmpty()}")
27+
print("큐에 숫자 0~9를 추가합니다.")
2828
for i in range(10):
2929
queue.enqueue(i)
30-
print("Queue size: ", queue.size())
31-
print("Queue peek : ", queue.peek())
32-
print("Dequeue...", queue.dequeue())
33-
print("Queue peek: ", queue.peek())
34-
print("Is the queue empty? ", queue.isEmpty())
30+
print(f"큐 크기: {queue.size()}")
31+
print(f"peek: {queue.peek()}")
32+
print(f"dequeue: {queue.dequeue()}")
33+
print(f"peek: {queue.peek()}")
34+
print(f"큐가 비었나요? {queue.isEmpty()}")
3535
print(queue)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Queue(object):
2+
def __init__(self):
3+
self.in_stack = []
4+
self.out_stack = []
5+
6+
def _transfer(self):
7+
while self.in_stack:
8+
self.out_stack.append(self.in_stack.pop())
9+
10+
def enqueue(self, item):
11+
return self.in_stack.append(item)
12+
13+
def dequeue(self):
14+
if not self.out_stack:
15+
self._transfer()
16+
if self.out_stack:
17+
return self.out_stack.pop()
18+
else:
19+
return "Queue empty!"
20+
21+
def size(self):
22+
return len(self.in_stack) + len(self.out_stack)
23+
24+
def peek(self):
25+
if not self.out_stack:
26+
self._transfer()
27+
if self.out_stack:
28+
return self.out_stack[-1]
29+
else:
30+
return "Queue empty!"
31+
32+
def __repr__(self):
33+
if not self.out_stack:
34+
self._transfer()
35+
if self.out_stack:
36+
return f"{self.out_stack}"
37+
else:
38+
return "Queue empty!"
39+
40+
def isEmpty(self):
41+
return not (bool(self.in_stack) or bool(self.out_stack))
42+
43+
44+
if __name__ == '__main__':
45+
queue = Queue()
46+
print(f"큐가 비었나요? {queue.isEmpty()}")
47+
print("큐에 숫자 0~9를 추가합니다.")
48+
for i in range(10):
49+
queue.enqueue(i)
50+
print(f"큐 크기: {queue.size()}")
51+
print(f"peek: {queue.peek()}")
52+
print(f"dequeue: {queue.dequeue()}")
53+
print(f"peek: {queue.peek()}")
54+
print(f"큐가 비었나요? {queue.isEmpty()}")
55+
print(queue)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Node(object):
2+
def __init__(self, value=None, pointer=None):
3+
self.value = value
4+
self.pointer = None
5+
6+
7+
class LinkedQueue(object):
8+
def __init__(self):
9+
self.head = None
10+
self.tail = None
11+
12+
def isEmpty(self):
13+
return not bool(self.head)
14+
15+
def dequeue(self):
16+
if self.head:
17+
value = self.head.value
18+
self.head = self.head.pointer
19+
return value
20+
else:
21+
print("Queue is empty, cannot dequeue.")
22+
23+
def enqueue(self, value):
24+
node = Node(value)
25+
if not self.head:
26+
self.head = node
27+
self.tail = node
28+
else:
29+
if self.tail:
30+
self.tail.pointer = node
31+
self.tail = node
32+
33+
def size(self):
34+
node = self.head
35+
num_nodes = 0
36+
while node:
37+
num_nodes += 1
38+
node = node.pointer
39+
return num_nodes
40+
41+
def peek(self):
42+
return self.head.value
43+
44+
def _print(self):
45+
node = self.head
46+
while node:
47+
print(node.value, end=" ")
48+
node = node.pointer
49+
print()
50+
51+
52+
if __name__ == '__main__':
53+
queue = LinkedQueue()
54+
print(f"큐가 비었나요? {queue.isEmpty()}")
55+
print("큐에 숫자 0~9를 추가합니다.")
56+
for i in range(10):
57+
queue.enqueue(i)
58+
print(f"큐가 비었나요? {queue.isEmpty()}")
59+
queue._print()
60+
61+
print(f"큐 크기: {queue.size()}")
62+
print(f"peek: {queue.peek()}")
63+
print(f"dequeue: {queue.dequeue()}")
64+
print(f"peek: {queue.peek()}")
65+
queue._print()

0 commit comments

Comments
 (0)