Queue using Two Stacks – Java Code & Algorithm

How to implement a queue using two stacks. In this tutorial, We are going to discuss an algorithm and java code to implement a queue using two stacks.

Get Minimum Element from Stack in O(1)

Design a stack in which we get minimum element from stack in O(1) (constant time).

For example : Suppose i push following elements in a stack.

9  ==> Top

Subscribe Our Tutorials

Get Latest Updates on Facebook

Then if i call getMin(), It will return 1 in O(1). In a stack, Element which pushed last in the stack is the first element to be popped out. Think for a moment how we can solve this problem.

Java Program to Reverse a String using Stack

Write a java program to reverse a string using stack data structure. Given an input string, We have to write a java code to reverse a string using stack.

In my previous tutorial, I have explained java program to reverse a string using recursion.

Before solving this problem, Let’s understand what is a stack data structure.

Find Next Greater Element in an Array – Java Code

Write a java program to find next greater element in an array. Given an input array, find the next greater element for every element of an array.The next greater element x is the first greater element on the right side of x in an array.

In this tutorial, we are going to use stack to find next greater element in an array.

For example:

Let’s take an array: [4, 2, 6, 8, 1]

4 => 6 (First next greater element of 4 is 6)
2 => 6 (Next greater element of 2 is 6)
6 => 8 (Next greater element of 6 is 8)
8 => null
1 => null

MCQ on Stack and Queue – Data Structure Practice Questions

MCQ on stack and queue data structure. A Stack and Queue data structure is very important in computer science. In this tutorial, you are going to learn about stack and queue data structure. Also you’ll find MCQ on stack and queues.

In my previous post i have discussed following things.

Stack program in C using Array.

Queue data structure and their implementation.

The purpose of this objective questions is to test how well you understand the concept of stack and queue.

Stack Program in C using an Array – Stack Tutorial Part – I

Write a stack program in C using an array. Implement a stack data structure using an Array. In this tutorial, You are going to learn about stack data structure and it’s implementation in C using an array.

C Program to Implement a Stack using Linked List

What is a Stack Data Structure?

A Stack is a Data Structure, in which insertion and deletion operations are allowed only at one end. It worked on LIFO (Last In First Out) Principle. In LIFO, an element which inserted last must be the first element to be removed.