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
- Which of the following is the primary goal of a Cross-Site Scripting (XSS) attack?
- Which of the following phases in the Software Development Life Cycle (SDLC) ensures that the final product meets the agreed-upon requirements and specifica...
- A programmer is implementing a data analysis tool that frequently needs to append elements to a collection. If an array is used, what is a potential perfor...
- KMP improves naive string matching by:
- Which of the following is a disadvantage of using arrays?
- In Python, what will be the output of the following code snippet, considering scope rules? x = 5 def func(): x = 10 def inner...
- Which of the following techniques is most efficient for finding the kth smallest element in a Binary Search Tree (BST)?
- Which of the following attacks can occur when a user is tricked into performing unintended actions on a trusted website without their knowledge?
- An algorithm with a time complexity of O(log n) means that its execution time:
- What is the space complexity of storing an adjacency matrix for a graph with V vertices and E edges?
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