Skip to content

Commit fa58c70

Browse files
committed
add description
1 parent b8e6343 commit fa58c70

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Diff for: Queue/queue.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Implement Queue Data Structure
2+
'''
3+
This implementation uses a Python list to store the items in the queue. The __init__() method initializes
4+
an empty list. The is_empty() method checks whether the list is empty or not by checking the length of
5+
the list. The enqueue() method adds an item to the back of the queue by appending it to the end of the
6+
list. The dequeue() method removes and returns the item at the front of the queue by using the pop()
7+
method to remove the first item in the list. If the list is empty, the method raises an IndexError.
8+
The peek() method returns the item at the front of the queue without removing it by returning the
9+
first item in the list. If the list is empty, the method raises an IndexError. The size() method
10+
returns the number of items in the list by returning the length of the list.
11+
'''
12+
class Queue:
13+
def __init__(self):
14+
"""
15+
Initializes an empty queue.
16+
"""
17+
self.items = []
18+
19+
def is_empty(self):
20+
"""
21+
Returns True if the queue is empty, False otherwise.
22+
"""
23+
return len(self.items) == 0
24+
25+
def enqueue(self, item):
26+
"""
27+
Adds the given item to the back of the queue.
28+
"""
29+
self.items.append(item)
30+
31+
def dequeue(self):
32+
"""
33+
Removes and returns the item at the front of the queue.
34+
If the queue is empty, raises an IndexError.
35+
"""
36+
if self.is_empty():
37+
raise IndexError("Queue is empty")
38+
return self.items.pop(0)
39+
40+
def peek(self):
41+
"""
42+
Returns the item at the front of the queue without removing it.
43+
If the queue is empty, raises an IndexError.
44+
"""
45+
if self.is_empty():
46+
raise IndexError("Queue is empty")
47+
return self.items[0]
48+
49+
def size(self):
50+
"""
51+
Returns the number of items in the queue.
52+
"""
53+
return len(self.items)

0 commit comments

Comments
 (0)