Advertisement
Shamel

ql332

Oct 31st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import static java.lang.System.out;
  4.  
  5. class QueueLab3Pt2 {
  6.     public static void main(String[] args) {
  7.         MyQueue2 q= new MyQueue2();
  8.         Scanner console = new Scanner(System.in);
  9.         int choice;
  10.         int number;
  11.         out.println(q);
  12.         while (true) {
  13.             out.println("Enter 1 to enqueue, 2 to dequeue, 3 to stop");
  14.             choice = console.nextInt();
  15.             switch (choice) {
  16.                 case 3: System.exit(0);
  17.                 case 1: out.println("Enter the number to enqueue");
  18.                     number = console.nextInt();
  19.                     q.add(number);
  20.                     q.display();
  21.                     break;
  22.                 case 2: out.println(q.remove() + " is dequeued");
  23.                     q.display();
  24.                     break;
  25.                 case 4:out.println(q.getSize());
  26.                     break;
  27.                 default: out.println("Invalid input");
  28.             }
  29.         }
  30.     }
  31.     }
  32.  
  33.  
  34. class MyQueue2{
  35.     private int[] list;
  36.     private int front;
  37.     private int back;
  38.     private int capacity;
  39.     private int size;
  40.  
  41.  
  42.     private boolean isFull()
  43.     {
  44.         if (back -1  == front)
  45.             return true;
  46.         else
  47.             return false;
  48.  
  49.  
  50.     }
  51.  
  52.     private void resize()
  53.     {
  54.  
  55.         int oldCapacity = capacity;
  56.         capacity *= 2;
  57.         int[ ] newItems = new int[capacity];
  58.         for (int i=0; i<size; i++) {
  59.             newItems[i] = list[front++];
  60.             if (front == oldCapacity)
  61.                 front = 0;
  62.         }
  63.         list = newItems;
  64.         front = 0;
  65.         back = size - 1;
  66.     }
  67.  
  68.     public MyQueue2() {
  69.         list=new int[4];
  70.         back=-1;
  71.         front=0;
  72.         size=0;
  73.         capacity=4;
  74.  
  75.     }
  76.  
  77.     public boolean isEmpty()
  78.     {
  79.         return (size==0);
  80.     }
  81.  
  82.     public void add(int item)
  83.     {
  84.         if (size == capacity)
  85.             resize();
  86.  
  87.         size++;
  88.         back = (back + 1)%capacity;
  89.         list[back] = item;
  90.  
  91.     }
  92.  
  93.     public int remove() {
  94.         if (size == 0) {
  95.             out.println("Queue is empty");
  96.             return -999;
  97.         }
  98.         int answer = list[front++];
  99.         size--;
  100.         if (size == 0) {
  101.             front = 0;
  102.             back = -1;
  103.  
  104.         }
  105.         if (front == capacity)
  106.             front = 0;
  107.         return answer;
  108.     }
  109.  
  110.     public int peek()
  111.     {
  112.         if(size==0){out.println("Queue is empty");return -999;}
  113.         return list[front];
  114.  
  115.     }
  116.  
  117.     public int getSize()
  118.     {
  119.  
  120.  
  121.         return size;
  122.     }
  123.  
  124.     public void display()
  125.     {
  126.         out.println(toString());
  127.  
  128.     }
  129.  
  130.     public String toString() {
  131.         if (size == 0)
  132.             return "[ ]" ;
  133.         String answer = "[ ";
  134.         int position = front;
  135.         for (int i=0; i<size; i++) {
  136.             answer += list[position++] + " ";
  137.             if (position == capacity) position = 0;
  138.         }
  139.         return answer + "] ";
  140.     }
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement