Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solver
- {
- public static void main(String args[])
- {
- }
- }
- class Queue
- {
- Stack st = new Stack();
- public void push(int d) // O(n)
- {
- Stack temp_st = new Stack();
- while (isEmpty() == false)
- {
- temp_st.push(st.pop().data);
- }
- temp_st.push(d);
- while (temp_st.isEmpty() == false)
- {
- st.push(temp_st.pop().data);
- }
- }
- public Node pop() // O(1)
- {
- if (isEmpty())
- {
- System.out.println("Can't pop, The Queue is Empty");
- return null;
- }
- return st.pop();
- }
- public Node front() // O(1)
- {
- if (isEmpty())
- {
- System.out.println("The Queue is Empty");
- return null;
- }
- return st.top();
- }
- public void display() // O(n)
- {
- if (isEmpty())
- {
- System.out.println("The Queue is Empty");
- return;
- }
- st.display();
- }
- public boolean isEmpty()
- {
- return st.isEmpty();
- }
- }
- class Node
- {
- int data;
- Node next;
- public Node(int d)
- {
- data = d;
- }
- }
- class Stack
- {
- Node Top;
- public void push(int d)
- {
- Node temp = new Node(d);
- temp.next = Top;
- Top = temp;
- return;
- }
- public Node pop()
- {
- Node temp = Top;
- Top = Top.next;
- return temp;
- }
- public Node top()
- {
- if (isEmpty())
- {
- System.out.println("The Stack is Empty");
- }
- return Top;
- }
- public void display()
- {
- if (isEmpty())
- {
- System.out.print("The Stack is Empty");
- }
- Node curr = Top;
- while (curr != null)
- {
- System.out.print(curr.data + " ");
- curr = curr.next;
- }
- System.out.println();
- return;
- }
- public boolean isEmpty()
- {
- return Top == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment