|
| 1 | +/*Queue is an abstract data structure, somewhat similar to Stacks. |
| 2 | +Unlike stacks, a queue is open at both its ends. One end is always |
| 3 | +used to insert data (enqueue) and the other is used to remove data |
| 4 | +(dequeue). Queue follows First-In-First-Out methodology, i.e., the |
| 5 | +data item stored first will be accessed first.*/ |
| 6 | + |
| 7 | +// this is the implementation of a basic queue |
| 8 | + |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Scanner; |
| 11 | +class QueUe{ |
| 12 | + |
| 13 | + int[] Qarray; |
| 14 | + int size; |
| 15 | + int front,rear; |
| 16 | + |
| 17 | + QueUe(int capacity){ |
| 18 | + size=capacity; |
| 19 | + Qarray = new int[capacity];//creating a queue as a array |
| 20 | + rear=-1;// initializing rear |
| 21 | + front=-1;//initializing front |
| 22 | + //queue is empty at first |
| 23 | + } |
| 24 | + |
| 25 | + void display(){ |
| 26 | + System.out.println(Arrays.toString(Qarray)); //print the queue |
| 27 | + } |
| 28 | + |
| 29 | + void value(){ |
| 30 | + System.out.println("front:"+front+"\t rare:"+rear); //to see the current rear & front |
| 31 | + } |
| 32 | + |
| 33 | + void peek() |
| 34 | + { |
| 35 | + if (isEmpty()) |
| 36 | + { |
| 37 | + System.out.println("Queue empty!");//if queue empty-> no element to peek |
| 38 | + return; |
| 39 | + } |
| 40 | + System.out.println("peek value is "+Qarray[front]);//peeking the front value |
| 41 | + } |
| 42 | + |
| 43 | + void enqueue(int val){ |
| 44 | + if (isFull()) |
| 45 | + { |
| 46 | + System.out.println("queue is full!"); |
| 47 | + return; //can not enter element if queue is full |
| 48 | + } |
| 49 | + if(rear==-1){ |
| 50 | + front++; //for 1st element put in queue from queue were empty |
| 51 | + } |
| 52 | + System.out.println("Adding " + val); |
| 53 | + |
| 54 | + rear++ ; //adding new element in rear -> rear++ |
| 55 | + Qarray[rear] = val; //putting the value |
| 56 | + } |
| 57 | + |
| 58 | + void dequeue() |
| 59 | + { |
| 60 | + // check for queue underflow |
| 61 | + if (isEmpty()) |
| 62 | + { |
| 63 | + System.out.println("queue is empty"); |
| 64 | + return; // if queue is empty -> no element to delete |
| 65 | + } |
| 66 | + |
| 67 | + System.out.println("Deleting " + Qarray[front]); |
| 68 | + if(front==rear){ |
| 69 | + front=-1; |
| 70 | + rear=-1;//when only 1 element was in queue , now deleted |
| 71 | + return; //now queue is empty |
| 72 | + } |
| 73 | + front++; //deleting element-> front ++ |
| 74 | + } |
| 75 | + |
| 76 | + Boolean isFull() |
| 77 | + { |
| 78 | + return (rear==size-1); // check if queue is full |
| 79 | + } |
| 80 | + |
| 81 | + Boolean isEmpty() |
| 82 | + { |
| 83 | + return (front==-1); // check if queue is empty |
| 84 | + } |
| 85 | +} |
| 86 | +class BasicQueue { |
| 87 | + |
| 88 | + public static void main(String[] args) |
| 89 | + { |
| 90 | + QueUe que = new QueUe(4); |
| 91 | + |
| 92 | + //just some random queue function sequence |
| 93 | + que.enqueue(5); |
| 94 | + que.peek(); |
| 95 | + que.dequeue(); |
| 96 | + que.peek(); |
| 97 | + que.enqueue(20); |
| 98 | + que.peek(); |
| 99 | + que.enqueue(4); |
| 100 | + que.value(); |
| 101 | + que.enqueue(17); |
| 102 | + que.enqueue(9); |
| 103 | + |
| 104 | + if (que.isFull()) |
| 105 | + System.out.println("Queue Is full"); |
| 106 | + |
| 107 | + que.peek(); |
| 108 | + que.dequeue(); |
| 109 | + que.dequeue(); |
| 110 | + que.peek(); |
| 111 | + |
| 112 | + System.out.println("Queue size is " + que.size); |
| 113 | + |
| 114 | + que.dequeue(); |
| 115 | + que.dequeue(); |
| 116 | + |
| 117 | + if (que.isEmpty()) |
| 118 | + System.out.println("Queue Is Empty"); |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments