Skip to main content

Implement Stack using two Queues

 



For implementing stack, we require two stacks, although it can be implemented using a single queue, but here we are going to see how to do it using two queues.


Queue is the reverse of Stack. 

Why? - Queue works on FIFO(First In First Out), and Stack works on LIFO (Last In First Out).


How we are going to do it?  

1. Initialize two queues queue_1, and queue_2.

2. Define two functions - push(item) and pop().

3. In push, what we are doing? - 

  • Append item in queue_2, then pop all elements from queue_1, and append to queue_2. 
  • Now, pop all elements from queue_2, and append to queue_1
     queue_2 is queue now because first item inserted will pop first.

4. In pop, if queue_1 is empty, return -1 (means no item in the queue). Else, pop elements from queue_1, and return the last element of queue_1.


Code - 

queue_1 = []

queue_2 = []

def push(x):

         global queue_1

        global queue_2

        queue_2.append(x)

        while len(queue_1)!=0:

                   top = queue_1.pop()

                  queue_2.append(top)

        

      while len(queue_2)!=0:

                    temp = queue_2.pop()

                   queue_1.append(temp)

        

  def pop():

           global queue_1

           global queue_2

           temp = -1

           if len(queue_1)!=0:

                  temp = queue_1.pop()

           

           return temp



I hope you understood the implementation, if you have any doubt do comment, I will help you out.

Comments

Popular posts from this blog

List of Questions asked by Morgan Stanley

  Coding Questions -  1.  .   Exchange kth node from start and kth node from end of a singly linked list. 2.    Given a number, you have to find next greater number which has same set of digits. 3.    Given a binary tree, you have to print level order traversal of the tree (left child then right child) but every next level has to be printed in next line. 4.    Longest Common Substring 5.    Spiral level order traversal 6.    Add two linked lists 7.    Write a function to find the mirror image of binary tree 8.    WAP to find the character which occurred maximum times in the character array 9.  Given a 2d matrix find an element in a matrix which is 0 and make the entire row and column to 0 10.  Find the minimum element in the rotated array of integers 11.  Find highest length substring such that there are equal number of 0’s and 1’sin array of 1’s and 0’s only 12.  Given a dictio...

Which is best: LeetCode vs HackerEarth ?

  Well, all the platforms almost are same except the interface. That's what I think. To conclude the result, let's compare them through the features they offer.  Features of Leetcode -  Topic - wise questions Comapny-wise questions Weekly contests Interview Preparation section Discuss section Features of HackerEarth Topic-wise problems Monthly contests Discuss section Hiring Contests Hackathons Earlier, when I started coding in my first year. I was fond of HackerEarth. But when I stoped using hackerearth and shifted to Leetcode, I like it more. It depends upon how much comfortable you are with the interface. But, it can't be denied that Leetcode offer more than HackerEarth. But for the coders who love hackathons, HackerEarth is best for them and for the people looking out for job opportunities. Do share your favorite through comments!

Pokemon App in Flutter using Pokemon API

In this Article, we will we building a pokemon app using pokemon api. I hope you have Flutter installed and somewhat familiar with Flutter and APIs. Calling APIs for the data and then displaying that extracted data as a beautiful UI is my favorite task. Let's have a look what we will be building today -  1. Add "http" dependency in pubspec.yaml 2. Add this code in main.dart -  import 'package:http/http.dart' as http; import 'dart:convert' ; import 'package:flutter/material.dart'; void main () { runApp ( MyApp ()); } class MyApp extends StatelessWidget { @override Widget build ( BuildContext context) { return MaterialApp ( debugShowCheckedModeBanner : false , home : MyHomePage (), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState () => _MyHomePageState (); } class _MyHomePageState extends State < MyHomePage > { Map pokeData; List pokeLis...