Import Java - Util.stack Public Class Linkedinterface (
Import Java - Util.stack Public Class Linkedinterface (
A queue can be implemented using two stacks. They are function call stack and user stack.
3. Given the StackInterface and the LinkedStack classes, write Java code to create a queue
using stack. Test your code by creating a queue Number that takes N numbers to enqueue
and then dequeue and prints them the numbers on the screen.
Source code:
import java.util.Stack;
public class Linkedinterface {
Stack<Integer> stack1;
top.push(new_data);
{
if (top == null) {
System.out.println("Stack Underflow");
System.exit(0);
return top.pop();
push(a.stack1, x);
int x, res = 0;
if (a.stack1.isEmpty()) {
System.out.println("Queue is Empty");
System.exit(0);
else if (a.stack1.size() == 1) {
return pop(a.stack1);
else {
x = pop(a.stack1);
res = dequeue(a);
push(a.stack1, x);
return res;
return 0;
enqueue(a, 89);
enqueue(a, 78);
enqueue(a, 30);
}
4. What is the complexity of queue implementation using stack?
Time Complexity:
Push = O(1)
Pop = O(N)
5. Is it possible to implement the stack data structure using a queue? Justify your answer.
a stack data structure with push and pop operations can be used to implement a queue
using instances of stack data structure and operations on them.