-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
95 lines (88 loc) · 2.58 KB
/
Queue.java
File metadata and controls
95 lines (88 loc) · 2.58 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.Scanner;
public class Queue {
private static int front, rear, size, capacity;
private static int queue[];
Queue(int size) {
front = rear = 0;
capacity = size;
queue = new int[capacity];
}
static void enqueue(int item) {
if (capacity == size) {
System.out.println("\nQueue is full\n");
return;
} else {
queue[rear] = item;
rear++;
size++;
System.out.println(+item + " successfully inserted in the queue");
}
return;
}
static void dequeue() {
if (front == rear) {
System.out.println("Queue Empty");
return;
} else {
System.out.println(queue[front] + " dequeued successfully");
for (int i = front; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
}
rear--;
size--;
return;
}
static void display() {
if (front == capacity) {
System.out.println("\nQueue is empty\n");
return;
} else {
for (int i = front; i < rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
}
return;
}
static void QueueFront() {
if (front == rear) {
System.out.println("\nQueue is empty\n");
return;
} else {
System.out.println("Front element of the queue is:- " + queue[front]);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the queue:- ");
int n = sc.nextInt();
new Queue(n);
for (;;) {
System.out.println("\nMenu:\n1.Enqueue \n2.Dequeue \n3.Display \n4.Front element\n5.Exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Enter the element to be inserted:- ");
int ele = sc.nextInt();
enqueue(ele);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
QueueFront();
break;
case 5:
sc.close();
return;
default:
System.out.println("Invalid choice");
break;
}
}
}
}