The document discusses double-ended queues (deques), which are linear data structures that allow efficient insertion and removal of elements from both ends. A deque provides the capabilities of both stacks and queues in a single structure by allowing new elements to be added and existing elements to be removed from either the front or rear. The key operations for a deque are described, including adding and removing elements from the front/rear. Example applications like browser history and undo operations in applications are given to illustrate the use of deques.
1. UNIT II LINEAR DATA
STRUCTURES – STACKS AND
QUEUES
TYPES OF QUEUES-
DOUBLE ENDED QUEUE
(DEQUE)
2. USAGE SCENARIO
• Consider the scenario of a queue in a theater. One by one, each
person gets the ticket and then the people move forward. When
all the tickets are sold and the theater reaches its maximum
capacity, people have to turn back and come out of the queue
from the rear end.
• This is replicated exactly by double ended queues, shortly
referred to as Deque.
3. What is DEQUE?
• A deque, also known as a double-ended queue, is an ordered collection of
items similar to the queue.
• It has two ends, a front and a rear, and the items remain positioned in the
collection.
• What makes a deque different is the unrestrictive nature of adding and
removing items.
• New items can be added at either the front or the rear. Likewise, existing
items can be removed from either end.
• In a sense, this hybrid linear structure provides all the capabilities of stacks
and queues in a single data structure.
4. • A deque is structured, as described above, as an ordered collection of items
where items are added and removed from either end, either front or rear.
• The deque operations are given below.
Deque() creates a new deque that is empty. It needs no parameters and
returns an empty deque.
add_front(item) adds a new item to the front of the deque. It needs the
item and returns nothing.
add_rear(item) adds a new item to the rear of the deque. It needs the
item and returns nothing.
remove_front() removes the front item from the deque. It needs no
parameters and returns the item. The deque is modified.
remove_rear() removes the rear item from the deque. It needs no
parameters and returns the item. The deque is modified.
is_empty() tests to see whether the deque is empty. It needs no
parameters and returns a boolean value.
size() returns the number of items in the deque. It needs no parameters
and returns an integer.
5.
6. Implementation of Double ended
Queue
• It will have the following methods:
push_back : inserts element at back
push_front : inserts element at front
pop_back : removes last element
pop_front : removes first element
get_back : returns last element
get_front : returns first element
empty : returns true if queue is empty
full : returns true if queue is full
• The ‘push_front’ and pop_front operations are based
on the FRONT pointer, whereas the push_back and
pop_back operations are based on the REAR pointer
7. Working: Push_Front Operations
If Queue is empty
Then,
Assign data in queue[0]
front queue[0]
rear queue[1]
increment size
Else,
Decrement front
Assign data to front
Increment size
8. Working: Push_Back Operations
If Queue is empty
Then,
Assign data in queue[0]
front queue[0]
rear queue[1]
increment size
Else,
Assign data to rear
Increment Rear
Increment size
9. Working: Pop_front Operations
If Queue is not empty
Then,
temp=Value at Front
increment front
decrement size
Clear memory location
Return Temp
Else,
Return 0 or deliver to specify queue is empty
10. Working: Pop_back Operations
If Queue is not empty
Then,
temp=Value at rear-1
decrement count
decrement rear
Clear memory location
Return Temp
Else,
Return 0 or deliver to specify queue is empty
11. Applications of Deque
• An internet browser’s history. Recently visited URLs are
supplementary to the front of the deque, and also the
uniform resource locator at the rear of the deque is
removed when some such range of insertions at the front.
• Another common application of the deque is storing a
computer code application’s list of undo operations.
• Have you ever see Money-Control App, it’ll show the
stocks you last visited, it’ll take away the stocks when a
while and can add the most recent ones.