-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
52 lines (40 loc) · 1.12 KB
/
Copy pathQueue.java
File metadata and controls
52 lines (40 loc) · 1.12 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
package queque.dsa.lab;
import java.util.LinkedList;
public class Queue {
private LinkedList<Integer> items;
public Queue() {
items = new LinkedList<>();
}
public boolean isEmpty() {
return items.isEmpty();
}
public void enqueue(int item) {
items.addLast(item);
}
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return -1;
}
return items.removeFirst();
}
public int peek() {
if (isEmpty()) {
System.out.println("Queue is empty.");
return -1;
}
return items.getFirst();
}
public int size() {
return items.size();
}
public static void main(String[] args) {
Queue queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println("Front element: " + queue.peek());
System.out.println("Dequeued element: " + queue.dequeue());
System.out.println("Queue size: " + queue.size());
}
}