3
3
4
4
# queue implementation
5
5
class Queue (object ):
6
- '''
7
-
8
- size : return the current size of the queue
9
- enqueue : insert an item into the queue
10
- dequeue : remove an item from the queue which was first inserted
11
- isEmpty : check if the queue is empty
12
-
13
- '''
14
-
15
6
def __init__ (self , limit = 10 ):
16
7
'''
17
8
@param: limit: queue size
@@ -22,16 +13,19 @@ def __init__(self, limit = 10):
22
13
self .limit = limit
23
14
self .size = 0
24
15
25
- # for printing the contents of the queue
26
16
def __str__ (self ):
27
17
return ' ' .join ([str (i ) for i in self .queue ])
28
18
29
- # to check if queue is empty
30
- def isEmpty (self ):
19
+ def size (self ):
20
+ ''' returns the current size of the queue '''
21
+ return self .size
22
+
23
+ def is_empty (self ):
24
+ ''' checks if the queue is empty '''
31
25
return self .size <= 0
32
26
33
- # to add an element from the rear end of the queue
34
27
def enqueue (self , data ):
28
+ ''' inserts an item into the queue '''
35
29
if self .size >= self .limit :
36
30
return - 1 # queue overflow
37
31
else :
@@ -45,9 +39,9 @@ def enqueue(self, data):
45
39
46
40
self .size += 1
47
41
48
- # to pop an element from the front end of the queue
49
42
def dequeue (self ):
50
- if self .isEmpty ():
43
+ ''' pops an item from the queue which was first inserted '''
44
+ if self .is_empty ():
51
45
return - 1 # queue underflow
52
46
else :
53
47
self .size -= 1
@@ -57,66 +51,55 @@ def dequeue(self):
57
51
self .rear = self .size - 1
58
52
return self .queue .pop (0 )
59
53
60
- # return the size of the queue
61
- def size (self ):
62
- return self .size
63
-
64
- # easily retrieve the source code of the Queue class
65
54
def get_code (self ):
66
55
import inspect
67
56
return inspect .getsource (Queue )
68
57
69
58
class Deque (object ):
70
- '''
71
-
72
- isEmpty : checks whether the deque is empty
73
- isFull : checks whether the deque is full
74
- insertRear : inserts an element at the rear end of the deque
75
- insertFront : inserts an element at the front end of the deque
76
- deleteRear : deletes an element from the rear end of the deque
77
- deleteFront : deletes an element from the front end of the deque
78
-
79
- '''
80
-
81
59
def __init__ (self , limit = 10 ):
82
60
self .queue = []
83
61
self .limit = limit
84
62
85
63
def __str__ (self ):
86
64
return ' ' .join ([str (i ) for i in self .queue ])
87
65
88
- # check if queue is empty
89
- def isEmpty ( self ):
66
+ def is_empty ( self ):
67
+ ''' checks whether the deque is empty '''
90
68
return len (self .queue ) <= 0
91
69
92
- # check if queue is full
93
- def isFull ( self ):
70
+ def is_full ( self ):
71
+ ''' checks whether the deque is full '''
94
72
return len (self .queue ) >= self .limit
95
73
96
- # for inserting at rear
97
- def insertRear ( self , data ):
98
- if self .isFull ():
74
+ def insert_rear ( self , data ):
75
+ ''' inserts an element at the rear end of the deque '''
76
+ if self .is_full ():
99
77
return
100
78
else :
101
79
self .queue .insert (0 , data )
102
80
103
- # for inserting at front end
104
- def insertFront ( self , data ):
105
- if self .isFull ():
106
- return
81
+ def insert_front ( self , data ):
82
+ ''' inserts an element at the front end of the deque '''
83
+ if self .is_full ():
84
+ return - 1
107
85
else :
108
86
self .queue .append (data )
109
87
110
- # deleting from rear end
111
- def deleteRear ( self ):
112
- if self .isEmpty ():
113
- return
88
+ def delete_rear ( self ):
89
+ ''' deletes an element from the rear end of the deque '''
90
+ if self .is_empty ():
91
+ return - 1
114
92
else :
115
93
return self .queue .pop (0 )
116
94
117
- # deleting from front end
118
- def deleteFront ( self ):
119
- if self .isFull ():
95
+ def delete_front ( self ):
96
+ ''' deletes an element from the front end of the deque '''
97
+ if self .is_full ():
120
98
return
121
99
else :
122
100
return self .queue .pop ()
101
+
102
+ def get_code (self ):
103
+ ''' returns the code of the current class '''
104
+ import inspect
105
+ return inspect .getsource (Deque )
0 commit comments