Javatpoint Logo
Javatpoint Logo

Implementation of Deque by Circular Array

Before going to the implementation of Deque using the circular array, first, let's understand what a queue is?

A queue is an orderly collection of items in which new items are added at one end, referred to as the "back", and current items are removed at the other end, referred to as the "front". When an element joins the queue, it begins at the back and works its way to the front, waiting for the next element to be removed. The queue's most recently added item must wait until the end of the collection. At the front is the object that has been in the collection the longest. FIFO, or first-in, first-out, is another name for this ordering approach. "First-come, first-served" is another term for it.

To regulate processes within a computer, operating systems use a variety of different queues. The next task is usually scheduled using a queuing method to run programs as rapidly as possible while serving as many people as possible. Also, keystrokes can sometimes outnumber the characters on the screen as we type, and this is because the computer is now occupied with other tasks. The keystrokes are queued in a buffer to be presented on the screen in the correct order later.

Enqueue is the process of adding an element to the back of the queue, and dequeue is the process of removing an element from the front. Other operations, such as a peek or front operation that returns the value of the next element to be dequeued without dequeuing it, may be permitted.

Now let's understand Deque. A deque or double-ended queue is an ordered group of items that functions similarly to a queue. It has two ends, a front and a back, and the items in the collection remain in place. The unrestrictive nature of adding and removing elements distinguishes a deque. New things can be added to the front or back of the house. Existing things can be removed from either end as well. It's worth noting that, while the Deque can mimic many of the properties of stacks and queues, it doesn't require the LIFO and FIFO orderings that those data structures enforce. It is up to you to use the addition and removal operations consistently. Here are the different operations that are supported by a deque data structure:

  • insertFront(x): The insertFront() function will insert an element x at the front of Deque. There is one parameter to the insertFront() function representing the data that we want to add to the front of the Deque data structure.
  • insertRear(x): The insertRear() function will insert an element x at the rear of Deque. There is one parameter to the insertRear() function representing the data that we want to add to the rear of the Deque data structure.
  • deleteFront(): The deleteFront() function will delete an element from the front of Deque
  • deleteRear(): The deleteRear() function will delete an element from the rear of Deque
  • getFront(): The getFront() function will return the element present at the front of Deque
  • getRear(): The getRear() function will return the element present at the rear of Deque
  • isEmpty(): The isEmpty() function will return whether or not the Deque is empty
  • isFull(): The isFull() function will return whether or not the Deque is full

Java Code

Now let's write a code in Java programming language to create a Deque data structure using a circular array.

Output:

The above code gives the following output:

Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
1
Enter the data:

10
Data Added Successfully.


Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
2
Enter the data :

25
Data Added Successfully.


Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
1
Enter the data :

45
Data Added Successfully.


Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
2
Enter the data :

60
Data Added Successfully.


Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
7
Data in Deque::
45 10 25 60 

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
3
Data at front:45


Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
4
Data at rear:60


Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
5
Data deleted from the front.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
7
Data in Deque::
10 25 60 

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
6
Data deleted from the rear.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data at the Front of Deque.
2. To Insert Data at the Rear of Deque.
3. To Print Data from the Front of Deque.
4. To Print Data from the Rear of Deque.
5. To Delete Data from the Front of Deque.
6. To Delete Data from the Rear of Deque.
7. To print the Deque.
7
Data in Deque::
10 25

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

n

In the above code, we have first created a Deque data structure and added into that Deque from both the ends using respective inserting functions for both the front and the rear end.

Once the data is successfully inserted into the Deque, the next step is to perform the delete operations at both ends of the deque data structure. There are two functions to delete data from both ends of the Deque.

After each operation, we have printed the contents of the deque data structure using the printDeque() function, which prints all the contents of the Deque beginning from the front and ending at the rear end of the Deque. And for creating the deque data, we have used the circular array.

Application

The work-stealing algorithm is an example of how a deque might be employed. [6] This technique is used to schedule tasks among many CPUs. For each processor, a separate deque with threads to be performed is kept. The processor obtains the first element from the Deque to start the next thread (using the "remove the first element" deque operation). If the current thread forks, the current thread is returned to the front of the Deque ("insert an element at the front"), and a new thread is started.

When one of the processors has completed the execution of its own threads (i.e., its Deque is empty), it can "steal" a thread from another processor by retrieving the final element from another processor's Deque ("remove the last element") and executing it. Intel's Threading Building Blocks (TBB) package uses the work-stealing mechanism for parallel programming.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA