Queue
Demonstration of queue using linked list in JAVA
package com.company;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// write your code here
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(1);
q.add(8);
q.add(4);
q.add(7);
q.add(89);
System.out.println("The queue is: " + q);
}
}
The queue is: [10, 1, 8, 4, 7, 89]
Process finished with exit code 0
Show peak of queue
package com.company;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// write your code here
Queue<Integer> q = new LinkedList<>();
q.add(6);
q.add(1);
q.add(8);
q.add(4);
q.add(7);
q.add(89);
System.out.println("The queue is: " + q);
int peek = q.peek();
System.out.println(peek);
}
}
The queue is: [6, 1, 8, 4, 7, 89]
6
vpackage com.company;
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// write your code here
Queue<Integer> q = new LinkedList<>();
q.add(6);
q.add(1);
q.add(8);
q.add(4);
q.add(7);
q.add(89);
System.out.println("The queue is: " + q);
int peek = q.peek();
System.out.println("Front element in queue : " +peek);
int removeElement = q.remove();//removeElement store the removed element from queue
System.out.println("After removing : "+q);
System.out.println("Element removed : "+removeElement);
}
}
The queue is: [6, 1, 8, 4, 7, 89]
Front element in queue : 6
After removing : [1, 8, 4, 7, 89]
Element removed : 6
size of the queue
int size = q.size();
System.out.println("The size of element is : "+size);
The size of element is : 5
Comments
Post a Comment