-
Notifications
You must be signed in to change notification settings - Fork 13
/
QueueNoSize.py
50 lines (36 loc) · 1.16 KB
/
QueueNoSize.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
class Queue:
def __init__(self):
self.items = []
def __str__(self): #time and space - O(1)
values = [str(x) for x in self.items]
return ' '.join(values)
def isEmpty(self): #time and space - O(1)
if self.items == []:
return True
else:
return False
def enqueue(self,value): #time-O(n^2) and space - O(1)
self.items.append(value)
return "The element is inserted at the end of the queue."
def dequeue(self): #time-O(n) and space - O(1)
if self.isEmpty():
return "No element found in the queue."
else:
return self.items.pop(0)
def peek(self): #time-O(1) and space - O(1)
if self.isEmpty():
return "No element found in the queue."
else:
return self.items[0]
def delete(self): #time-O(1) and space - O(1)
self.items = None
customQueue = Queue()
#print(customQueue.isEmpty())
customQueue.enqueue(1)
customQueue.enqueue(2)
customQueue.enqueue(3)
customQueue.enqueue(4)
print(customQueue.dequeue())
print(customQueue.peek())
print(customQueue)
customQueue.delete()