-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathStackandQueue.java
More file actions
54 lines (43 loc) · 937 Bytes
/
StackandQueue.java
File metadata and controls
54 lines (43 loc) · 937 Bytes
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
package stackandqueue;
public class queue {
private int[] data;
private static final int defcap = 10;
private int size = 0;
private int rear = 0, front = 0;
queue() {
this(defcap);
}
queue(int capacity) {
this.data = new int[capacity];
this.rear = capacity - 1;
}
public void enqueue(int val) {
if (this.size == this.data.length) {
System.out.println("queue is full");
return;
}
this.rear = (this.rear + 1) % this.data.length;
this.data[this.rear] = val;
this.size++;
}
public int dequeue() {
if (this.size == 0) {
System.out.println("queue is empty");
return -1;
}
int val = this.data[this.front];
this.front = (this.front + 1) % this.data.length;
this.size--;
return val;
}
public void display() {
int i = front;
int c = 0;
while (c < this.size) {
System.out.print(this.data[i] + " ");
i = (i + 1) % this.data.length;
c++;
}
System.out.println();
}
}