Boostr



0 votes
17 views
Deque , is a short abbreviation of  Double Ended QUEue . Java provides Deque class , which is found in java.util package . Here we try to apply the functionality of deque in the console based java programming .  Deque is an abstract data  type which is a generalize form of queue .
In deque the elements can be added or removed  only from two  ends i.e   beginning and tail end .
A-Steal job scheduling algorithm is an example of deque implementation.

Here in this below example , you can select the add ,remove , display or exit  functionality by choosing the number in front of the options .
If you selected the add  or remove functionality then there will be two more actions you can perform, you can either add or remove them from end or at beginning . For front end insertion/deletion press a or rear end insertion /deletion press b .

Here in deque there are two pointers associated with each link .

                                     --->                 --->                --->              -->                 
                          5                          2                      4                34                27
                                     <---                 <---                 <---              <--

Actually in java there are built in methods to perform the task which are done by deque .

Deque can perform the following functions :                                             JAVA  built in methods

* Insertion at  the front                                                                                       offerFirst
* Insertion  at the back                                                                                       offerLast
* Remove element from the front                                                                         pollFirst
* Remove element from the back                                                                         pollLast

These methods are found in the java.util.ArrayDequeue class in java . If you see the oracle docs then you will   find two more methods in the class addFirst() and addLast()  , then one obvious question arises what is the difference between offerFirst and addFirst  methods .
The difference is in the return type of the two methods , addFirst() methods return type is void while the return type of the offerFirst() method is boolean . So we can use offerFirst() method in the condition of the loops as well . Similar difference also exist between the addLast() and offerLast() method of the arraydeque class .

There is also  removeFirst() and removeLast() methods in the arraydeque class , then the question arises why we choose pollFirst() instead of removeFirst() .
The only difference between the pollFirst and removeFirst methods  is that the pollFirst() method has extra feature  , that is , if the deque is empty  then the pollFirst() method will return null .
Similar is the reason of difference between pollLast() and removeLast() methods .

Pseudo code :

1. Choose add,remove,display or exit from the menu
2. If add
             2.1  choose rear or front
                     inserted
3. If remove
             3.1  choose front end or rear end
                     deleted
4. If display
              show all elements in the deque to the console
5. If choose exit
               then program shuts down

Here we intentionally use the skip method to read the characters from the console . The functionality of the skip method is to skip the specified number of inputs from the input stream .
Suppose n is 2
so skip(5 * n) ,
that is skip(10)  , so it will skip the 10 characters in the input stream or in other words start reading the input stream from the 11 th character .

One example of deque implementation is the A-Steal job scheduling algorithm . This algorithm implements task scheduling for various processors .
asked Aug 24 in Java Interview Questions by Amrendra | 17 views



Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Related Questions



Welcome to Knowledge Boostr, where you can ask questions and receive answers from other members of the community.
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers


23,471 questions
40,135 answers
12 comments
23,609 users