-
Notifications
You must be signed in to change notification settings - Fork 209
/
Queue_ll.py
36 lines (36 loc) · 992 Bytes
/
Queue_ll.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
# Implementation of the Queue ADT using a linked list.
class lQueue :
# Creates an empty queue.
def __init__( self ):
self._qhead = None
self._qtail = None
self._count = 0
# Returns True if the queue is empty.
def isEmpty( self ):
return self._qhead is None
# Returns the number of items in the queue.
def __len__( self ):
return self._count
# Adds the given item to the queue.
def enqueue( self, item ):
node = _QueueNode( item )
if self.isEmpty() :
self._qhead = node
else :
self._qtail.next = node
self._qtail = node
self._count += 1
# Removes and returns the first item in the queue.
def dequeue( self ):
assert not self.isEmpty(), "Cannot dequeue from an empty queue."
node = self._qhead
if self._qhead is self._qtail :
self._qtail = None
self._qhead = self._qhead.next
self._count -= 1
return node.item
# Private storage class for creating the linked list nodes.
class _QueueNode( object ):
def __init__( self, item ):
self.item = item
self.next = None