I have come up with the below code but somehow its not working as expected. What could be the reason of this. Idea is to make a pair of count and item to be pushed to the priority queue and then just pop.But how to implement this in python3?
import heapq
class Stack:
def __init__(self):
self.dict ={}
self.cnt = 0
self.hq = []
def push(self, item):
self.cnt += 1
self.heappush(hq, {self.cnt:item})
def pop(self):
self.cnt -= 1
return self.heappop(hq)
if __name__ == '__main__':
s = Stack()
s.push(10)
s.push(20)
s.push(30)
print(s.pop())
I am receiving the below error.
Traceback (most recent call last):
File "/home/shrivatsa/Documents/E/Learning-Path/Python/Algorithms and Datastructure/stackpriorityqueue.py", line 19, in <module>
s.push(10)
File "/home/shrivatsa/Documents/E/Learning-Path/Python/Algorithms and Datastructure/stackpriorityqueue.py", line 11, in push
self.heappush(hq, {self.cnt:item})
AttributeError: 'Stack' object has no attribute 'heappush'