Mahmoud_Hawara

Stack using Two Queues

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