Question
What will be the output of the following queue implementation using two stacks? class QueueUsingStacks { Stack s1 = new Stack (); Stack s2 = new Stack (); void enqueue ( int
What will be the output of the following queue implementation using two stacks? class QueueUsingStacks { Stack s1 = new Stack (); Stack s2 = new Stack (); void enqueue ( int
x) { s1.push(x); } int dequeue () { if (s2.isEmpty()) { while (!s1.isEmpty()) { s2.push(s1.pop()); } } if (!s2.isEmpty()) { return s2.pop(); } throw new RuntimeException ( "Queue is empty!" ); } } QueueUsingStacks queue = new QueueUsingStacks (); queue.enqueue( 1 ); queue.enqueue( 2 ); queue.enqueue( 3 ); System.out.println(queue.dequeue()); queue.enqueue( 4 ); System.out.println(queue.dequeue());
More Data Structure Questions
- In system design, what is the primary purpose of a feasibility study?
- What is a 'Binary Search Tree' (BST) and what is its key property?
- What is the maximum number of nodes in a binary tree of height 'h' (where root is at height 0)?
- Which data structure uses LIFO (Last In, First Out) principle?
- Which of the following is NOT a type of graph representation?
- Consider the following Python code for calculating the length of the LCS: def lcs_length(text1, text2): m = len(text1) n = len(text2) dp = ...
- Which algorithm is used for minimum spanning tree?
- An algorithm with a time complexity of O(log n) means that its execution time:
- Deletion in Red-Black Tree may require:
- Which of the following techniques is most efficient for finding the kth smallest element in a Binary Search Tree (BST)?
Hey! Ask a query
Please enter email id
The email must be a valid email address.
Please enter Mobile Number
Please enter valid Mobile Number
Please enter your Doubt