-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode5.java
More file actions
64 lines (52 loc) · 1.38 KB
/
Code5.java
File metadata and controls
64 lines (52 loc) · 1.38 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
import java.util.EmptyStackException;
class Queue {
private int maxSize;
private int[] queueArray;
private int front;
private int rear;
private int nItems;
public Queue(int size) {
maxSize = size;
queueArray = new int[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
public void enqueue(int value) {
if (rear == maxSize - 1) {
System.out.println("Queue is full. Cannot enqueue element.");
return;
}
queueArray[++rear] = value;
nItems++;
}
public void dequeue() {
if (nItems == 0) {
throw new EmptyStackException();
}
int temp = queueArray[front++];
if (front == maxSize) {
front = 0;
}
nItems--;
System.out.println("Dequeue element is : "+temp);
}
public void peek() {
if (nItems==0) {
throw new EmptyStackException();
}
System.out.println("Queue Front is : "+ queueArray[front]);
}
}
public class Code5 {
public static void main(String[] args) {
Queue queue = new Queue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.peek();
queue.dequeue();
queue.dequeue();
queue.peek();
}
}