forked from Audacity21/DSA-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDouble-ended-queue.java
72 lines (62 loc) · 2.09 KB
/
Double-ended-queue.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
63
64
65
66
67
68
69
70
71
72
import java.util.ArrayList;
import java.util.List;
public class DoubleEndedQueueImpl {
private List<Integer> deque = new ArrayList<Integer>();
public void insertFront(int item){
//add element at the beginning of the queue
System.out.println("adding at front: "+item);
deque.add(0,item);
System.out.println(deque);
}
public void insertRear(int item){
//add element at the end of the queue
System.out.println("adding at rear: "+item);
deque.add(item);
System.out.println(deque);
}
public void removeFront(){
if(deque.isEmpty()){
System.out.println("Deque underflow!! unable to remove.");
return;
}
//remove an item from the beginning of the queue
int rem = deque.remove(0);
System.out.println("removed from front: "+rem);
System.out.println(deque);
}
public void removeRear(){
if(deque.isEmpty()){
System.out.println("Deque underflow!! unable to remove.");
return;
}
//remove an item from the beginning of the queue
int rem = deque.remove(deque.size()-1);
System.out.println("removed from front: "+rem);
System.out.println(deque);
}
public int peakFront(){
//gets the element from the front without removing it
int item = deque.get(0);
System.out.println("Element at first: "+item);
return item;
}
public int peakRear(){
//gets the element from the rear without removing it
int item = deque.get(deque.size()-1);
System.out.println("Element at rear: "+item);
return item;
}
public static void main(String a[]){
DoubleEndedQueueImpl deq = new DoubleEndedQueueImpl();
deq.insertFront(34);
deq.insertRear(45);
deq.removeFront();
deq.removeFront();
deq.removeFront();
deq.insertFront(21);
deq.insertFront(98);
deq.insertRear(5);
deq.insertFront(43);
deq.removeRear();
}
}