-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearQueue.java
More file actions
94 lines (82 loc) · 2.3 KB
/
Copy pathLinearQueue.java
File metadata and controls
94 lines (82 loc) · 2.3 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
import java.util.Scanner;
class Queue {
int[] queue;
int front, rear, size;
Queue(int size) {
this.size = size;
queue = new int[size];
front = -1;
rear = -1;
}
// Insert element
void enqueue(int value) {
if (rear == size - 1) {
System.out.println("Queue Overflow");
return;
}
if (front == -1)
front = 0;
queue[++rear] = value;
System.out.println(value + " inserted");
}
// Delete element
void dequeue() {
if (front == -1 || front > rear) {
System.out.println("Queue Underflow");
return;
}
System.out.println(queue[front] + " deleted");
front++;
if (front > rear) {
front = -1;
rear = -1;
}
}
// Display queue
void display() {
if (front == -1) {
System.out.println("Queue is empty");
return;
}
System.out.print("Queue: ");
for (int i = front; i <= rear; i++) {
System.out.print(queue[i] + " ");
}
System.out.println();
}
}
public class LinearQueue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter queue size: ");
int n = sc.nextInt();
Queue q = new Queue(n);
System.out.println("\nSelect from the listed options-");
while (true) {
System.out.println("1. Insert");
System.out.println("2. Delete");
System.out.println("3. Display");
System.out.println("4. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter value: ");
q.enqueue(sc.nextInt());
break;
case 2:
q.dequeue();
break;
case 3:
q.display();
break;
case 4:
System.out.println("Program ended.");
sc.close();
return;
default:
System.out.println("Invalid choice");
}
}
}
}