1
- from pydatastructs .linear_data_structures import DynamicOneDimensionalArray
2
- from pydatastructs .utils .misc_util import NoneType
1
+ from pydatastructs .linear_data_structures import DynamicOneDimensionalArray , SinglyLinkedList
2
+ from pydatastructs .utils .misc_util import NoneType , LinkedListNode
3
3
from copy import deepcopy as dc
4
4
5
5
__all__ = [
@@ -15,17 +15,12 @@ class Queue(object):
15
15
implementation : str
16
16
Implementation to be used for queue.
17
17
By default, 'array'
18
- Currently only supports 'array'
19
- implementation.
20
18
items : list/tuple
21
19
Optional, by default, None
22
20
The inital items in the queue.
23
- For array implementation.
24
21
dtype : A valid python type
25
22
Optional, by default NoneType if item
26
- is None, otherwise takes the data
27
- type of DynamicOneDimensionalArray
28
- For array implementation.
23
+ is None.
29
24
30
25
Examples
31
26
========
@@ -51,6 +46,11 @@ def __new__(cls, implementation='array', **kwargs):
51
46
return ArrayQueue (
52
47
kwargs .get ('items' , None ),
53
48
kwargs .get ('dtype' , int ))
49
+ elif implementation == 'linkedlist' :
50
+ return LinkedListQueue (
51
+ kwargs .get ('items' , None ),
52
+ kwargs .get ('dtype' , NoneType )
53
+ )
54
54
raise NotImplementedError (
55
55
"%s hasn't been implemented yet." % (implementation ))
56
56
@@ -64,7 +64,9 @@ def popleft(self, *args, **kwargs):
64
64
65
65
@property
66
66
def is_empty (self ):
67
- return None
67
+ raise NotImplementedError (
68
+ "This is an abstract method." )
69
+
68
70
69
71
class ArrayQueue (Queue ):
70
72
@@ -122,3 +124,59 @@ def __str__(self):
122
124
for i in range (self .front , self .rear + 1 ):
123
125
_data .append (self .items ._data [i ])
124
126
return str (_data )
127
+
128
+
129
+ class LinkedListQueue (Queue ):
130
+
131
+ __slots__ = ['front' , 'rear' , 'size' , '_dtype' ]
132
+
133
+ def __new__ (cls , items = None , dtype = NoneType ):
134
+ obj = object .__new__ (cls )
135
+ obj .queue = SinglyLinkedList ()
136
+ obj ._dtype = dtype
137
+ if items is None :
138
+ pass
139
+ elif type (items ) in (list , tuple ):
140
+ if len (items ) != 0 and dtype is NoneType :
141
+ obj ._dtype = type (items [0 ])
142
+ for x in items :
143
+ if type (x ) == obj ._dtype :
144
+ obj .queue .append (x )
145
+ else :
146
+ raise TypeError ("Expected %s but got %s" % (obj ._dtype , type (x )))
147
+ else :
148
+ raise TypeError ("Expected type: list/tuple" )
149
+ obj .front = obj .queue .head
150
+ obj .rear = obj .queue .tail
151
+ obj .size = obj .queue .size
152
+ return obj
153
+
154
+ def append (self , x ):
155
+ if self ._dtype is NoneType :
156
+ self ._dtype = type (x )
157
+ elif type (x ) is not self ._dtype :
158
+ raise TypeError ("Expected %s but got %s" % (self ._dtype , type (x )))
159
+ self .size += 1
160
+ self .queue .append (x )
161
+ if self .front is None :
162
+ self .front = self .queue .head
163
+ self .rear = self .queue .tail
164
+
165
+ def popleft (self ):
166
+ if self .is_empty :
167
+ raise ValueError ("Queue is empty." )
168
+ self .size -= 1
169
+ return_value = self .queue .pop_left ()
170
+ self .front = self .queue .head
171
+ self .rear = self .queue .tail
172
+ return return_value
173
+
174
+ @property
175
+ def is_empty (self ):
176
+ return self .size == 0
177
+
178
+ def __len__ (self ):
179
+ return self .size
180
+
181
+ def __str__ (self ):
182
+ return str (self .queue )
0 commit comments