Skip to content

Commit 1765955

Browse files
iamrajivczgdp1807
andauthored
Added Queue data structure (#85)
Co-authored-by: Gagandeep Singh <[email protected]>
1 parent 563857b commit 1765955

File tree

5 files changed

+167
-18
lines changed

5 files changed

+167
-18
lines changed

AUTHORS

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
Gagandeep Singh<[email protected]>
2-
Kartikei Mittal<[email protected]>
3-
4-
Rohan Singh<[email protected]>
5-
Tarun Singh Tomar<[email protected]>
6-
Saptashrungi Birajdar<[email protected]>
1+
Gagandeep Singh <[email protected]>
2+
Kartikei Mittal <[email protected]>
3+
4+
Rohan Singh <[email protected]>
5+
Tarun Singh Tomar <[email protected]>
6+
Saptashrungi Birajdar <[email protected]>
7+
Rajiv Ranjan Singh <[email protected]>

pydatastructs/miscellaneous_data_structures/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
Stack,
1515
)
1616
__all__.extend(stack.__all__)
17+
18+
from .queue import (
19+
Queue,
20+
)
21+
__all__.extend(queue.__all__)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray
2+
from pydatastructs.utils.misc_util import NoneType
3+
from copy import deepcopy as dc
4+
5+
__all__ = [
6+
'Queue'
7+
]
8+
9+
class Queue(object):
10+
"""Representation of queue data structure.
11+
12+
Parameters
13+
==========
14+
15+
implementation : str
16+
Implementation to be used for queue.
17+
By default, 'array'
18+
Currently only supports 'array'
19+
implementation.
20+
items : list/tuple
21+
Optional, by default, None
22+
The inital items in the queue.
23+
For array implementation.
24+
dtype : A valid python type
25+
Optional, by default NoneType if item
26+
is None, otherwise takes the data
27+
type of DynamicOneDimensionalArray
28+
For array implementation.
29+
30+
Examples
31+
========
32+
33+
>>> from pydatastructs import Queue
34+
>>> q = Queue()
35+
>>> q.append(1)
36+
>>> q.append(2)
37+
>>> q.append(3)
38+
>>> q.popleft()
39+
1
40+
>>> len(q)
41+
2
42+
43+
References
44+
==========
45+
46+
.. [1] https://en.wikipedia.org/wiki/Queue_(abstract_data_type)
47+
"""
48+
49+
def __new__(cls, implementation='array', **kwargs):
50+
if implementation == 'array':
51+
return ArrayQueue(
52+
kwargs.get('items', None),
53+
kwargs.get('dtype', int))
54+
raise NotImplementedError(
55+
"%s hasn't been implemented yet."%(implementation))
56+
57+
def append(self, *args, **kwargs):
58+
raise NotImplementedError(
59+
"This is an abstract method.")
60+
61+
def popleft(self, *args, **kwargs):
62+
raise NotImplementedError(
63+
"This is an abstract method.")
64+
65+
@property
66+
def is_empty(self):
67+
return None
68+
69+
class ArrayQueue(Queue):
70+
71+
__slots__ = ['front']
72+
73+
def __new__(cls, items=None, dtype=NoneType):
74+
if items is None:
75+
items = DynamicOneDimensionalArray(dtype, 0)
76+
else:
77+
dtype = type(items[0])
78+
items = DynamicOneDimensionalArray(dtype, items)
79+
obj = object.__new__(cls)
80+
obj.items, obj.front = items, -1
81+
if items.size == 0:
82+
obj.front = -1
83+
else:
84+
obj.front = 0
85+
return obj
86+
87+
def append(self, x):
88+
if self.is_empty:
89+
self.front = 0
90+
self.items._dtype = type(x)
91+
self.items.append(x)
92+
93+
def popleft(self):
94+
if self.is_empty:
95+
raise ValueError("Queue is empty.")
96+
return_value = dc(self.items[self.front])
97+
front_temp = self.front
98+
if self.front == self.rear:
99+
self.front = -1
100+
else:
101+
if (self.items._num - 1)/self.items._size < \
102+
self.items._load_factor:
103+
self.front = 0
104+
else:
105+
self.front += 1
106+
self.items.delete(front_temp)
107+
return return_value
108+
109+
@property
110+
def rear(self):
111+
return self.items._last_pos_filled
112+
113+
@property
114+
def is_empty(self):
115+
return self.__len__() == 0
116+
117+
def __len__(self):
118+
return self.items._num
119+
120+
def __str__(self):
121+
_data = []
122+
for i in range(self.front, self.rear + 1):
123+
_data.append(self.items._data[i])
124+
return str(_data)

pydatastructs/miscellaneous_data_structures/stack.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
from pydatastructs.linear_data_structures import DynamicOneDimensionalArray
2+
from pydatastructs.utils.misc_util import _check_type, NoneType
23
from copy import deepcopy as dc
34

45
__all__ = [
56
'Stack'
67
]
78

8-
_check_type = lambda a, t: isinstance(a, t)
9-
NoneType = type(None)
10-
119
class Stack(object):
12-
"""Respresentation of stack data structure
10+
"""Representation of stack data structure
1311
1412
Parameters
1513
==========
@@ -24,13 +22,13 @@ class Stack(object):
2422
The inital items in the stack.
2523
For array implementation.
2624
dtype : A valid python type
27-
Optional, by default int if item
25+
Optional, by default NoneType if item
2826
is None, otherwise takes the data
29-
type of OneDimensionalArray
27+
type of DynamicOneDimensionalArray
3028
For array implementation.
3129
32-
Example
33-
=======
30+
Examples
31+
========
3432
3533
>>> from pydatastructs import Stack
3634
>>> s = Stack()
@@ -74,19 +72,20 @@ def peek(self):
7472

7573
class ArrayStack(Stack):
7674

77-
__slots__ = ['items', 'dtype']
75+
__slots__ = ['items']
7876

79-
def __new__(cls, items=None, dtype=int):
77+
def __new__(cls, items=None, dtype=NoneType):
8078
if items is None:
8179
items = DynamicOneDimensionalArray(dtype, 0)
8280
else:
8381
items = DynamicOneDimensionalArray(dtype, items)
8482
obj = object.__new__(cls)
85-
obj.items, obj.dtype = \
86-
items, items._dtype
83+
obj.items = items
8784
return obj
8885

8986
def push(self, x):
87+
if self.is_empty:
88+
self.items._dtype = type(x)
9089
self.items.append(x)
9190

9291
def pop(self):
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pydatastructs.miscellaneous_data_structures import Queue
2+
from pydatastructs.utils.raises_util import raises
3+
4+
def test_Queue():
5+
6+
q1 = Queue(implementation='array', items=[0])
7+
q1.append(1)
8+
q1.append(2)
9+
q1.append(3)
10+
assert str(q1) == '[0, 1, 2, 3]'
11+
assert len(q1) == 4
12+
assert q1.popleft() == 0
13+
assert q1.popleft() == 1
14+
assert len(q1) == 2
15+
assert q1.popleft() == 2
16+
assert q1.popleft() == 3
17+
assert len(q1) == 0
18+
19+
q1 = Queue()
20+
raises(ValueError, lambda: q1.popleft())

0 commit comments

Comments
 (0)