Solved queue.get() effeciency problem#463
Open
huzaifarehman1 wants to merge 3 commits intoprabhupant:masterfrom
Open
Solved queue.get() effeciency problem#463huzaifarehman1 wants to merge 3 commits intoprabhupant:masterfrom
huzaifarehman1 wants to merge 3 commits intoprabhupant:masterfrom
Conversation
O9864
approved these changes
Sep 14, 2025
Himaanshuraikwar
left a comment
There was a problem hiding this comment.
class Queue:
class __Node:
"""Private node class for the linked list logic."""
def init(self, element, next_node=None):
self.ele = element
self.next = next_node
def __init__(self):
self.head = None # Points to the front (where we dequeue)
self.tail = None # Points to the end (where we enqueue)
self.length = 0
def is_empty(self):
return self.length == 0
def put(self, item):
"""Add item to the end of the queue (Enqueue)."""
new_node = self.__Node(item)
if self.is_empty():
self.head = new_node
self.tail = new_node
else:
# Link the old tail to the new node, then move tail pointer
self.tail.next = new_node
self.tail = new_node
self.length += 1
def get(self):
"""Remove and return the item from the front (Dequeue)."""
if self.is_empty():
return None
# Capture the data from the head
dequeued_item = self.head.ele
# Move the head pointer to the next node
self.head = self.head.next
# If the queue is now empty, reset tail to None
if self.head is None:
self.tail = None
self.length -= 1
return dequeued_item
def rotate(self, rotation):
"""Moves the front element to the back 'n' times."""
if self.is_empty():
return
for _ in range(rotation):
self.put(self.get())
def size(self):
return self.length
def __len__(self):
return self.length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
i changed entire logic of queue class and rewrote it using liked list to achieve O(1) time complexity while also keeping the same space compeltxity
Fixes #462
Type of change
Checklist: