-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue.py
63 lines (55 loc) · 1.76 KB
/
Queue.py
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
class Node:
def __init__ (self, elem, link=None):
self.data = elem
self.link = link
class CircularLinkedQueue:
def __init__( self ):
self.tail = None
def isEmpty( self ): return self.tail == None
def clear( self ): self.tail = None
def peek( self ):
if not self.isEmpty():
return self.tail.link.data
def enqueue( self, item ):
node = Node(item, None)
if self.isEmpty() :
node.link = node
self.tail = node
else :
node.link = self.tail.link
self.tail.link = node
self.tail = node
def dequeue( self ):
if not self.isEmpty():
data = self.tail.link.data
if self.tail.link == self.tail :
self.tail = None
else:
self.tail.link = self.tail.link.link
return data
def size( self ):
if self.isEmpty() : return 0
else :
count = 1
node = self.tail.link
while not node == self.tail:
node = node.link
count += 1
return count
def display( self, msg='CircularLinkedQueue:' ):
print(msg, end='')
if not self.isEmpty() :
node = self.tail.link
while not node == self.tail :
print(node.data,"->", end=' ')
node = node.link
print(node.data,"->None", end=' ')
print()
myQ = CircularLinkedQueue()
while True:
A = int(input("양의 정수를 입력하세요(종료:-1):"))
if A == -1:
break
else:
myQ.enqueue(A)
myQ.display()