Advertisement
Shamel

ql333333

Oct 27th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 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.         q.remove();
  14.         q.remove();
  15.         q.remove();
  16.         q.remove();
  17.         q.remove();
  18.  
  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.         }
  82.         else if (back==list.length-1)
  83.         {
  84.             resize();
  85.  
  86.             list[back]=item;
  87.             back++;
  88.  
  89.         }
  90.         else {
  91.  
  92.  
  93.             list[back] = item;
  94.             back++;
  95.  
  96.         }
  97.     }
  98.  
  99.     public int remove()
  100.     {
  101.         if(size-1>=0) {
  102.             size--;
  103.             int temp=back;
  104.             back--;
  105.  
  106.  
  107.             return list[temp];
  108.  
  109.         }
  110.         else
  111.             out.println("The queue is already empty .. i cant remove stuff out of thin air... smh");
  112.  
  113.         return -999;
  114.  
  115.  
  116.     }
  117.  
  118.     public int peek()
  119.     {
  120.         return list[front];
  121.  
  122.     }
  123.  
  124.     public int getSize()
  125.     {
  126.         return list.length;
  127.     }
  128.  
  129.     public void display()
  130.     {
  131.         if (front== back)
  132.             out.println("dont print out an empty queue.. ITS EMPTY MANN..");
  133.         else
  134.         for (int k=0; k<=back-1;k++)
  135.         {
  136.             out.print(list[k]+ "  ");
  137.         }
  138.  
  139.     }
  140.  
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement