From 78a39d96f042c0f596242dfba40ddd0dac06e1ee Mon Sep 17 00:00:00 2001 From: Huzaifa Rehman Date: Sun, 14 Sep 2025 11:25:06 +0500 Subject: [PATCH 1/3] added new function and class to queue --- .../queue/__pycache__/queue.cpython-312.pyc | Bin 0 -> 2904 bytes data_structures/queue/queue.py | 36 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 data_structures/queue/__pycache__/queue.cpython-312.pyc diff --git a/data_structures/queue/__pycache__/queue.cpython-312.pyc b/data_structures/queue/__pycache__/queue.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b42aac97a5f47005785531944466dcfa416568eb GIT binary patch literal 2904 zcmcIm&2JM&6rZ)%&iW%HKp-J0Flnn2tBGAzs@DN zO|S`7s-|jEK?C9tj`UPf{*PXI2}o7lRS%VV;zp86NSylKtnHd4^wyF5_RYte_ulV) zjDP9vO%oW`&7aFZB?*haKp0seOnV%Qc1p0u$>Y|slE$^UL~GLZ zY42;K9?Qk;t{u|RP=wVDxT~ZwQi?IH6j$0}pe^3frZatoloBifG|7@cdsq)pgBd_m zECn>p(m*ryOfDNHKCAH>w=Qdhq$vzFrH-VQN3CfY2+IyAvjU3(U$cXn;Nw*K6~0)u z0xF$~<643SKD`oH_PoriOFs8pXP$!uE{#)*E}#;;)vt26H=xsgP^L~GDfgJn)5{)5 zPU&?Qt-7Q&$r)bjc#?~j1!LS(7v+=^3(f~+PT?jlP_iu|c*VcYBcsO{k3=HFBJXB= z*Yy`AJ?{t^OiXzb5p%f~&d!>@7VUPbVEUlOIQ(|j1Jh!DIn(H9LtV3o2F-%~$K8VVaKT1g6Ho9hmXLr-2oORyTZUo zcNiE3rE^;Cb%g;8e+D-Yh=pF}77LRu_hy4~s9&`l_mL(tuqjcTDkGKEjRS3@B8U#+ znFyjk2ZmPl+o|T!LHJ~vIJ=WlX76t?1yM*GG6&z?gK6`S$yWf}#;10f+w)j}DtgqP zp5xFnV9qPmpiUumJwH$-fhi(5^bijkfbVk4`*#^_1fPzz06XI5Ys^hBnt-UC{=# z`359{9qXzdXy1*i7K2`f?E*TOLM3Vm=>Mdcm1jC`sW_k78nT`-N6TLd}t=4RW^C71a)J zwBD>}hVP9$1!S50aq#Hf8~0Cc6D>8mI@!$juU}ldc=zhYwR_j@mwwICtvGD#O*HgT z`5!%&;0-M79#5+dQPVL}x0b%PqjBR4m82&#r!xI8s8eXj4 zNO@~|8m)W}3BD=CTvqhs1iQ1q{v{3rf!9b0wcZ+hgv1FXosftLoEDIvM`8d;=amu& znj(mx`~b)b*@`6-#)|{V#PKZxWV>Ha9NW(7i4)t|9^eiof%L}`quYj-7}-ezi7v#u zpxdbHLTy3d%eG73OXH*HA^ljOe@f|_z&wv@n)V`gS{vCSNL-C1(qCU#)|y27*7(Z! ICaqNb19&TA_W%F@ literal 0 HcmV?d00001 diff --git a/data_structures/queue/queue.py b/data_structures/queue/queue.py index a02ae3ab..8c24d80d 100644 --- a/data_structures/queue/queue.py +++ b/data_structures/queue/queue.py @@ -6,14 +6,33 @@ class Queue: Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first. """ + class __Node: # new addition + """ used for double linked list logic of queue""" + """ should not available outside the class""" + def __init__(self,element,next = None,prev = None): + self.ele = element + self.next = next + self.prev = prev - def __init__(self): - self.entries = [] - self.length = 0 - self.front = 0 + def __init__(self): # modified part + + self.head = None # points to last pushed element (enqueue part) + self.length = 0 # number of elements in it + self.Tail = None # points to first pushed element (dequeue part) + + def is_empty(self): # new addition + """check is queue object is empty or not + return boolean + """ + return self.length<=0 + + def put(self, item): # modified part + """add item to queue - def put(self, item): - self.entries.append(item) + Args: + item (any): item to be pushed + """ + self.entries.append self.length += 1 def get(self): @@ -30,3 +49,8 @@ def rotate(self, rotation): def size(self): return self.length + + def __len__(self): # new addition + return self.length +q = Queue() +q.__Node() \ No newline at end of file From eaaa1998003857f4986e6e29f0e36ac605814ece Mon Sep 17 00:00:00 2001 From: Huzaifa Rehman Date: Sun, 14 Sep 2025 11:33:30 +0500 Subject: [PATCH 2/3] completed put function --- data_structures/queue/queue.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/data_structures/queue/queue.py b/data_structures/queue/queue.py index 8c24d80d..9438e2fe 100644 --- a/data_structures/queue/queue.py +++ b/data_structures/queue/queue.py @@ -32,9 +32,23 @@ def put(self, item): # modified part Args: item (any): item to be pushed """ - self.entries.append - self.length += 1 + temp = self.__Node(item) # create new node + + if self.is_empty(): + # initialize the doubly linked list + self.head = temp + self.Tail = temp + + self.length += 1 + return + # already initialized so just add the item + self.head.next = temp # point to next element + self.head = self.head.next # move pointer to next element which is last pushed element + + self.length += 1 + return + def get(self): if self.length <= 0: return @@ -51,6 +65,4 @@ def size(self): return self.length def __len__(self): # new addition - return self.length -q = Queue() -q.__Node() \ No newline at end of file + return self.length \ No newline at end of file From 474bde002c2930c847674189b650286c18f08f28 Mon Sep 17 00:00:00 2001 From: Huzaifa Rehman Date: Sun, 14 Sep 2025 11:43:10 +0500 Subject: [PATCH 3/3] completed --- data_structures/queue/queue.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/data_structures/queue/queue.py b/data_structures/queue/queue.py index 9438e2fe..e196b0cb 100644 --- a/data_structures/queue/queue.py +++ b/data_structures/queue/queue.py @@ -6,13 +6,12 @@ class Queue: Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first. """ - class __Node: # new addition + class __Node: # new addition part """ used for double linked list logic of queue""" """ should not available outside the class""" def __init__(self,element,next = None,prev = None): self.ele = element self.next = next - self.prev = prev def __init__(self): # modified part @@ -20,7 +19,7 @@ def __init__(self): # modified part self.length = 0 # number of elements in it self.Tail = None # points to first pushed element (dequeue part) - def is_empty(self): # new addition + def is_empty(self): # new addition part """check is queue object is empty or not return boolean """ @@ -49,13 +48,22 @@ def put(self, item): # modified part self.length += 1 return - def get(self): - if self.length <= 0: - return + def get(self): # modified part + """ pop the tail element and move the pointer one step ahead + """ + if self.is_empty(): + return + self.length -= 1 - de_queued = self.entries[self.front] - self.entries = self.entries[1:] - return de_queued + + temp = self.Tail + nex = self.Tail.next + self.Tail.next = None + self.Tail = nex + + return temp.ele + + def rotate(self, rotation): for i in range(rotation): @@ -64,5 +72,5 @@ def rotate(self, rotation): def size(self): return self.length - def __len__(self): # new addition + def __len__(self): # new addition part return self.length \ No newline at end of file