Advertisement
Shamel

ql333333333

Oct 29th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import static java.lang.System.out;
  2.  
  3. public class QueueLab3 {
  4.     public static void main(String[] args) {
  5.  
  6.         MyQueue q=new MyQueue();
  7.         q.add(1);
  8.         q.add(2);
  9.         q.add(3);
  10.         q.add(4);
  11.         q.add(5);
  12.         q.add(6);
  13.  
  14.  
  15.         q.remove();
  16.         q.remove();
  17.         q.remove();
  18.         out.println(q.getSize());
  19.  
  20.  
  21.         q.display();
  22.  
  23.     }
  24.  
  25.  
  26. }
  27.  
  28. class MyQueue
  29. {
  30.     private int list[];
  31.     private int front;
  32.     private int back;
  33.     private int capacity;
  34.     private int size;
  35.  
  36.  
  37.     private boolean isFull()
  38.     {
  39.         if (back -1  == front)
  40.             return true;
  41.         else
  42.             return false;
  43.  
  44.  
  45.     }
  46.  
  47.     private void resize()
  48.     {
  49.  
  50.         int list2[]=new int[list.length*2];
  51.         for(int k=0;k<list.length;k++)
  52.             list2[k]=list[k];
  53.         capacity=list.length*2;
  54.         list=list2;
  55.  
  56.     }
  57.  
  58.     public MyQueue() {
  59.         list=new int[4];
  60.         back=0;
  61.         front=0;
  62.         size=0;
  63.         capacity=4;
  64.  
  65.     }
  66.  
  67.     public boolean isEmpty()
  68.     {
  69.         return (size==0);
  70.     }
  71.  
  72.     public void add(int item)
  73.     {
  74.         size++;
  75.         if (back==0) {
  76.  
  77.             list[0] = item;
  78.             back++;
  79.         }
  80.  
  81.         else if (back==list.length-1)
  82.         {
  83.             resize();
  84.  
  85.             list[back]=item;
  86.             back++;
  87.         }
  88.         else {
  89.             list[back] = item;
  90.             back++;
  91.         }
  92.     }
  93.  
  94.     public int remove()
  95.     {
  96.         if(size-1>=0) {
  97.             size--;
  98.             int temp=back;
  99.             back--;
  100.  
  101.  
  102.             return list[temp];
  103.  
  104.         }
  105.         else
  106.             out.println("The queue is already empty .. i cant remove stuff out of thin air... smh");
  107.  
  108.         return -999;
  109.  
  110.  
  111.     }
  112.  
  113.     public int peek()
  114.     {
  115.         return list[front];
  116.  
  117.     }
  118.  
  119.     public int getSize()
  120.     {
  121.  
  122.  
  123.         return back-front;
  124.     }
  125.  
  126.     public void display()
  127.     {
  128.         if (front== back)
  129.             out.println("dont print out an empty queue.. ITS EMPTY MANN..");
  130.         else
  131.             for (int k=0; k<=back-1;k++)
  132.             {
  133.                 out.print(list[k] + "  ");
  134.             }
  135.  
  136.     }
  137.  
  138.  
  139.  
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement