Mahmoud_Hawara

Stack using one Queue

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