-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_ll.java
More file actions
98 lines (75 loc) · 1.38 KB
/
queue_ll.java
File metadata and controls
98 lines (75 loc) · 1.38 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class QueueUsingLL<T> {
private Node<T> front;
private Node<T> rear;
private int size;
public QueueUsingLL() {
front = null;
rear = null;
size = 0;
}
int size(){
return size;
}
boolean isEmpty(){
return size == 0;
}
T front() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
return front.data;
}
void enqueue(T element){
Node<T> newNode = new Node<>(element);
if(rear == null){
front = newNode;
rear = newNode;
}else{
rear.next = newNode;
rear = newNode;
}
size++;
}
T dequeue() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
T temp = front.data;
front = front.next;
if(size == 1){
rear = null;
}
size--;
return temp;
}
}
public class Node<T> {
T data;
Node<T> next;
Node(T data){
this.data = data;
next = null;
}
}
public class QueueUse {
public static void main(String[] args) {
// QueueUsingArray queue = new QueueUsingArray(3);
QueueUsingLL<Integer> queue = new QueueUsingLL<>();
for(int i = 1; i <= 5; i++){
queue.enqueue(i);
// try {
// queue.enqueue(i);
// } catch (QueueFullException e) {
//
// }
}
while(! queue.isEmpty()){
try {
System.out.println(queue.dequeue());
} catch (QueueEmptyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}