Mahmoud_Hawara

Queue with Two Stacks

Nov 15th, 2022
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. public class Solver
  2. {
  3.     public static void main(String args[])
  4.     {
  5.        
  6.     }
  7. }
  8.  
  9. class Queue
  10. {
  11.     Stack st = new Stack();
  12.  
  13.     public void push(int d)  // O(n)
  14.     {
  15.         Stack temp_st = new Stack();
  16.         while (isEmpty() == false)
  17.         {
  18.             temp_st.push(st.pop().data);
  19.         }
  20.         temp_st.push(d);
  21.         while (temp_st.isEmpty() == false)
  22.         {
  23.             st.push(temp_st.pop().data);
  24.         }
  25.     }
  26.  
  27.     public Node pop()   // O(1)
  28.     {
  29.         if (isEmpty())
  30.         {
  31.             System.out.println("Can't pop, The Queue is Empty");
  32.             return null;
  33.         }
  34.         return st.pop();
  35.     }
  36.  
  37.     public Node front() // O(1)
  38.     {
  39.         if (isEmpty())
  40.         {
  41.             System.out.println("The Queue is Empty");
  42.             return null;
  43.         }
  44.         return st.top();
  45.     }
  46.  
  47.     public void display()   // O(n)
  48.     {
  49.         if (isEmpty())
  50.         {
  51.             System.out.println("The Queue is Empty");
  52.             return;
  53.         }
  54.         st.display();
  55.     }
  56.  
  57.     public boolean isEmpty()
  58.     {
  59.         return st.isEmpty();
  60.     }
  61. }
  62.  
  63. class Node
  64. {
  65.     int data;
  66.     Node next;
  67.  
  68.     public Node(int d)
  69.     {
  70.         data = d;
  71.     }
  72. }
  73.  
  74. class Stack    
  75. {
  76.     Node Top;
  77.  
  78.     public void push(int d)
  79.     {
  80.         Node temp = new Node(d);
  81.         temp.next = Top;
  82.         Top = temp;
  83.         return;
  84.     }
  85.  
  86.     public Node pop()
  87.     {
  88.         Node temp = Top;
  89.         Top = Top.next;
  90.         return temp;
  91.     }
  92.  
  93.     public Node top()
  94.     {
  95.         if (isEmpty())
  96.         {
  97.             System.out.println("The Stack is Empty");
  98.         }
  99.         return Top;
  100.     }
  101.  
  102.     public void display()
  103.     {
  104.         if (isEmpty())
  105.         {
  106.             System.out.print("The Stack is Empty");
  107.         }
  108.         Node curr = Top;
  109.         while (curr != null)
  110.         {
  111.             System.out.print(curr.data + " ");
  112.             curr = curr.next;
  113.         }
  114.         System.out.println();
  115.         return;
  116.     }
  117.  
  118.     public boolean isEmpty()
  119.     {
  120.         return Top == null;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment