-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue.java
More file actions
129 lines (106 loc) · 3.24 KB
/
Queue.java
File metadata and controls
129 lines (106 loc) · 3.24 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.NoSuchElementException;
/*
[data|next] ---> [data|next] ---> [data|next] ---> [data|next] ---> null
^ ^
Front Rear
Queue is linear data structure used for storing the data.
it's an ordered list in which insertion are done at one end, called rear and deletion are done at other end called as front.
The first element inserted is the first one to be deleted
Hence, it is called as FIRST IN FIST OUT(FIFO) list.
*/
public class Queue {
// deletion at front end / point to the first element
private ListNode front;
// insertion at rear end / point to the last element
private ListNode rear;
// for getting the length
private int length;
// creating a constructor to assign default values
public Queue(){
this.front = null;
this.rear = null;
this.length = 0;
}
// For creating a node
private class ListNode{
private int data;
private ListNode next;
public ListNode(int data){
this.data = data;
this.next = null;
}
}
// return length of the Queue
public int length(){
return length;
}
// check queue is empty or not.
public boolean isEmpty(){
return length == 0;
}
// Create a method to insert element in the queue
public void enQueue(int data){
ListNode temp = new ListNode(data);
if(isEmpty()){
front = temp;
} else {
rear.next = temp;
}
rear = temp;
length++;
}
// create a method to delete the element from front of queue
public int deQueue(){
if(isEmpty()){
throw new NoSuchElementException("queue is empty");
}
int result = front.data;
front = front.next;
if(front == null){
rear = null;
}
length--;
return result;
}
// return the first element in the queue
public int first(){
if(isEmpty()){
throw new NoSuchElementException("queue is empty");
}
return front.data;
}
// return the last element in the queue
public int last() {
if (isEmpty()) {
throw new NoSuchElementException("queue is empty");
}
return rear.data;
}
// Method to print the queue;
public void print(){
if(isEmpty()){
return;
}
ListNode current = front;
while (current != null){
System.out.print(current.data + " --> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
Queue queue = new Queue();
queue.enQueue(10);
queue.enQueue(15);
queue.enQueue(20);
queue.print();
System.out.println("First Element->"+queue.first());
System.out.println("First Element->"+queue.last());
System.out.println("Element removed->"+queue.deQueue());
queue.print();
System.out.println("Element removed->"+queue.deQueue());
queue.print();
System.out.println("Element removed->"+queue.deQueue());
queue.print();
}
}