-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queues_Operations.java
62 lines (57 loc) · 1.44 KB
/
Queues_Operations.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
public class Lab_2_Queue {
private Node front, rear;
private int queueSize;
private class Node {
int data;
Node next;
}
public Lab_2_Queue(){
front = null;
rear = null;
queueSize = 0;
}
public boolean isEmpty(){
return (queueSize == 0);
}
public int dequeue(){
int data = front.data;
front = front.next;
if (isEmpty()){
rear = null;
}
queueSize--;
System.out.println("Element " + data+ " removed from the queue");
return data;
}
public void enqueue(int data){
Node oldRear = rear;
rear = new Node();
rear.data = data;
rear.next = null;
if (isEmpty()){
front = rear;
}
else {
oldRear.next = rear;
}
queueSize++;
System.out.println("Element " + data+ " added to the queue");
}
public void print_frontRear() {
System.out.println("Front of the queue:" + front.data + " Rear of the queue:" + rear.data);
}
}
class Queue{
public static void main(String a[]){
Lab_2_Queue queue = new Lab_2_Queue();
queue.enqueue(15);
queue.enqueue(8);
queue.print_frontRear();
queue.enqueue(38);
queue.enqueue(32);
queue.dequeue();
queue.dequeue();
queue.enqueue(4);
queue.print_frontRear();
}
}