This repository has been archived by the owner on Aug 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathQueueUsingArray.java
80 lines (77 loc) · 2.1 KB
/
QueueUsingArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package queueusingarray;
public class QueueUsingArray {
public static void main(String[] args) {
/* Create a queue of size 5 */
Queue q = new Queue(5);
/* Inserting elements in the queue */
q.queueEnque(1);
q.queueEnque(2);
q.queueEnque(3);
q.queueEnque(4);
q.queueEnque(5);
/* Print queue elements */
q.queueDisplay();
/* Delete elements */
q.queueDeque();
q.queueDeque();
/* Print queue elements */
q.queueDisplay();
}
}
class Queue{
private static int front,rear,size;
private static int queue[];
Queue(int c){
front=rear=0;
size=c;
queue = new int[size];
}
/* Function to insert an element at the rear of the queue */
static void queueEnque(int data){
/* Check whether queue is full or not */
if(size==rear){
System.out.println("Queue is Full");
}
/* Insert an element at the rear */
else{
queue[rear]=data;
rear++;
}
}
/* Function to delete an element from the front of the queue */
static void queueDeque(){
/* Check if queue is empty */
if(front==rear){
System.out.println("Queue is Empty");
}
/* Shift all the elements from index 2 till rear to the right by one */
else{
for(int i=0;i<rear-1;i++){
queue[i]=queue[i+1];
}
}
/* Decrement Rear */
rear--;
}
/* Print Queue elements */
static void queueDisplay(){
if(front==rear){
System.out.println("Queue is Empty");
}
/* Traverse front to rear and print elements */
System.out.println ();
for(int i=front;i<rear;i++){
System.out.print(queue[i]+" ");
}
}
/* Print front of the queue */
static void queuefront(){
if(front==rear){
System.out.println("Queue is Empty");
}
System.out.println("Front Element"+" "+queue[front]);
}
}
Output: -
1 2 3 4 5
3 4 5