Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I wanted to implement a queue by using 2 stacks in Python. I am a newbie and I need all the help I can get.

1 Answer

0 votes
by (108k points)

Kindly refer to the below code for implementing queue in Python

class Queue(object):

    def __init__(self):

        self.instack=[]

        self.outstack=[]

    def enqueue(self,element):

        self.instack.append(element)

    def dequeue(self):

        if not self.outstack:

            while self.instack:

                self.outstack.append(self.instack.pop())

        return self.outstack.pop()

q=Queue()

for i in range(10):

    q.enqueue(i)

for i in xrange(10):

    print q.dequeue(),

Related questions

0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Dec 19, 2020 in Python by ashely (50.2k points)
+1 vote
1 answer
0 votes
1 answer
asked Jul 30, 2019 in Python by Eresh Kumar (45.3k points)

Browse Categories

...