-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCircularQueue.py
81 lines (65 loc) · 2.3 KB
/
CircularQueue.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class Queue:
def __init__(self, maxsize): # Time Complexity = O(1) Space Complexity = O(N)
self.items = maxsize * [None]
self.maxsize = maxsize
self.start = -1
self.top = -1
def __str__(self):
values = [str(x) for x in self.items]
return ' '.join(values)
def isFull(self): # Time Complexity = O(1) Space Complexity = O(1)
if self.top + 1 == self.start:
return True
elif self.start == 0 and self.top + 1 == self.maxsize:
return True
else:
return False
def isEmpty(self): # Time Complexity = O(1) Space Complexity = O(1)
if self.top == -1:
return True
else:
return False
def enqueue(self,value):# Time Complexity = O(1) Space Complexity = O(1)
if self.isFull():
return "The queue is full, cannot insert element."
else:
if self.top + 1 == self.maxsize:
self.top = 0
else:
self.top += 1
if self.start == -1:
self.start = 0
self.items[self.top] = value
return "The element is inserted at the end of the queue."
def dequeue(self):# Time Complexity = O(1) Space Complexity = O(1)
if self.isEmpty():
return "Queue is empty. Dequeue cannot be done in empty queue."
else:
firstElement = self.items[self.start]
start = self.start
if self.start == self.top:
self.start = -1
self.top = -1
elif self.start + 1 == self.maxsize:
self.start = 0
else:
self.start += 1
self.items[start] = None
return firstElement
def peek(self):# Time Complexity = O(1) Space Complexity = O(1)
if self.isEmpty():
return "Queue is empty. Cannot perform peek in empty queue."
else:
return self.items[self.start]
def delete(self):
self.items = self.maxsize * [None]
self.top = -1
self.start = -1
customQueue = Queue(3)
#print(customQueue.isFull())
#print(customQueue.isEmpty())
customQueue.enqueue(1)
customQueue.enqueue(2)
customQueue.enqueue(4)
print(customQueue.dequeue())
print(customQueue.peek())