LO1M1
LO1M1
LO1M1
M1: Illustrate, with an example, a concrete data structure for a First in First
out (FIFO) queue.
Queue data structure:
The queue is waiting list where the order represents First-in-First-out (FIFO). It is an
interface in java.util package. It is an ordered list where the use of insertion is last in list
and use of deletion is first in the list which is following FIFO. There are methods of
queue which are given below:
o add(): This is method that is used for adding elements in the last. This is inherited
from Collection Interface.
o offer(): This is method through you can add an specific element in the queue
without disturbing any other queue element.
o element(): This is an method in which queue is empty then queue throw
exception such as NoSuchElementException.
o peek(): This is method where you can view the head of the queue without
deletion when the queue empty then it returns an null value.
o remove(): This is method where you remove and returns the head of queue and
then it throw exception same as element() which is NoSuchElementException.
o poll(): This is method similar to remove() method, but in this you remove the
head of the queue and then get return value of null.
o size(): This is method which counts number of elements present in the queue.
The common class used class in queue is given below:
o Priority Queue: The priority queue is imported java.util.PriorityQueue package.
It is an unbounded priority queue and follows the First-in-First-out but doesn’t
allow null value. The priority queue declaration is given below:
PriorityQueue<String> q1 = new PriorityQueue();
This study source was downloaded by 100000850558056 from CourseHero.com on 08-27-2022 06:25:33 GMT -05:00
https://www.coursehero.com/file/67333480/LO1M1docx/
From the above image we can easily understand, in which if you are First in queue front
list you will be first to dequeue or delete and if you are in last then you been just insert in
the queue list.
For example:- below is code of First-in-First-out in java:
package queue_fifo;
import java.util.LinkedList;
import java.util.Queue;
This study source was downloaded by 100000850558056 from CourseHero.com on 08-27-2022 06:25:33 GMT -05:00
https://www.coursehero.com/file/67333480/LO1M1docx/
String name=q.remove();
System.out.println("Removed name from queue|"+name+"| New queue list"+q);
//Removing one element from the queue with poll method
name=q.poll();
System.out.println("Removed name from queue|"+name+"| New queue list"+q);
}
}
Below is image code being executed in NetBeans Java compiler and output:
This study source was downloaded by 100000850558056 from CourseHero.com on 08-27-2022 06:25:33 GMT -05:00
https://www.coursehero.com/file/67333480/LO1M1docx/
Powered by TCPDF (www.tcpdf.org)