LO1M1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LO1: Examine abstract data types, concrete data structures and algorithms.

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();

o Linked List Queue: The linked list queue is imported java.util.LinkedList


package. It does an basic operation of linking the elements in ordered queue for
the FIFO. The linked list queue declaration is given below:
Queue<Integer> q2 = new LinkedList();
Illustrating queue First-in-First-out with example:
 As we know that queue is like waiting for queue on rail tickets counter, in the queue is
there concept of First-in-First-out which is just like rail ticket counter queue in which first
in the queue list goes first out of the queue.
 Below is image of FIFO for easy to understand:

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;

public class Queue_FIFO {

public static void main(String[] args) {


//Initialize the queue Linked List
Queue<String> q= new LinkedList<>();

// Adding Elements to queue which means Enqueue


q.add("Jainish");
q.add("Siddhesh");
q.add("Ruchi");
q.add("Kunwar");
q.add("Jatin");
//Printing queue list
System.out.println("Waiting Queue List"+q);
//Removing one element from the queue which means Dequeue

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)