-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathQueue.java
More file actions
59 lines (50 loc) · 825 Bytes
/
Queue.java
File metadata and controls
59 lines (50 loc) · 825 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
55
56
57
58
59
class Node{
int val;
Node next;
Node(int data){
this.val = data;
this.next = null;
}
}
class Queue{
Node front;
Node rear;
Queue(int data){
this.front = new Node(data);
this.rear = this.front;
}
void add(int data){
Node newNode = new Node(data);
this.rear.next = newNode;
this.rear = newNode;
}
void delete(){
Node delNode = this.front;
this.front = delNode.next;
}
int peekFirst(){
return this.front.val;
}
int peekLast(){
return this.rear.val;
}
}
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
Queue q = new Queue(5);
System.out.println(q.peekFirst());
System.out.println(q.peekLast());
q.add(6);
q.add(7);
q.add(8);
q.add(9);
q.add(10);
q.add(6);
q.delete();
q.delete();
System.out.println(q.peekFirst());
System.out.println(q.peekLast());
}
}