forked from joeyajames/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queues implementaion.py
64 lines (51 loc) · 1.21 KB
/
Queues implementaion.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
# implemented by Linked list
class Node(object):
def __init__(self, item = None):
self.item = item
self.next = None
self.previous = None
class Queue(object):
def __init__(self):
self.length = 0
self.head = None
self.tail = None
def enqueue(self, x):
newNode = Node(x)
if self.head == None:
self.head = self.tail = newNode
else:
self.tail.next = newNode
newNode.previous = self.tail
self.tail = newNode
self.length += 1
def dequeue (self):
item = self.head.item
self.head = self.head.next
self.length -= 1
if self.length == 0:
self.last = None
return item
#################################################
# implemented by array
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, data):
self.items.append(data)
def dequeue(self):
return self.items.pop(0)
def display(self):
ar = []
for i in self.items:
ar.append(i)
return ar
que = Queue()
que.enqueue('google')
que.enqueue('youtube')
que.enqueue('udemy')
que.enqueue('udacity')
que.dequeue()
que.dequeue()
print(que.display())