forked from akgmage/data-structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.py
More file actions
53 lines (48 loc) · 1.9 KB
/
queue.py
File metadata and controls
53 lines (48 loc) · 1.9 KB
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
# Implement Queue Data Structure
'''
This implementation uses a Python list to store the items in the queue. The __init__() method initializes
an empty list. The is_empty() method checks whether the list is empty or not by checking the length of
the list. The enqueue() method adds an item to the back of the queue by appending it to the end of the
list. The dequeue() method removes and returns the item at the front of the queue by using the pop()
method to remove the first item in the list. If the list is empty, the method raises an IndexError.
The peek() method returns the item at the front of the queue without removing it by returning the
first item in the list. If the list is empty, the method raises an IndexError. The size() method
returns the number of items in the list by returning the length of the list.
'''
class Queue:
def __init__(self):
"""
Initializes an empty queue.
"""
self.items = []
def is_empty(self):
"""
Returns True if the queue is empty, False otherwise.
"""
return len(self.items) == 0
def enqueue(self, item):
"""
Adds the given item to the back of the queue.
"""
self.items.append(item)
def dequeue(self):
"""
Removes and returns the item at the front of the queue.
If the queue is empty, raises an IndexError.
"""
if self.is_empty():
raise IndexError("Queue is empty")
return self.items.pop(0)
def peek(self):
"""
Returns the item at the front of the queue without removing it.
If the queue is empty, raises an IndexError.
"""
if self.is_empty():
raise IndexError("Queue is empty")
return self.items[0]
def size(self):
"""
Returns the number of items in the queue.
"""
return len(self.items)