# data[].attributes.attachment_link.title
مقدمة عن الـ queue - introduction to queue in data structure

# data[].attributes.attachment_link.title
أنواع الـ queue - queue types in data structure

# data[].attributes.attachment_caption
Linear queue : normal queue where insertion takes place at the front of the queue and deletion takes place at the end .

# data[].attributes.attachment_caption
Circular queue : The last element is connected to the first element , It is also called as Ring Buffer .

# data[].attributes.attachment_caption
Every element has a priority associated with it.

# data[].attributes.attachment_caption
Double ended queue (Deque) : Insertion and deletion operations can be done at both Front and end of the queue .

# data[].attributes.attachment_link.title
Queue in Data Structure & Basic Operations for Queue | Simplilearn

# data[].attributes.attachment_link.description
Guide to Queue in Data Structure. Understand how to create queue in data structure along with Basic Operations like enqueue, dequeue, peek, isfull & isnull. Learn More.

# data[].attributes.attachment_link.readable_text
Deque (Doubly Ended Queue) in Python is implemented using the module "collections". Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to a list that provides O(n) time complexity.

Types of Restricted Deque Input

Input Restricted Deque: Input is limited at one end while deletion is permitted at both ends.

Output Restricted Deque: output is limited at one end but insertion is permitted at both ends.

Example: Python code to demonstrate deque
Output
deque(['name', 'age', 'DOB'])

Operations on deque

Example 1: Appending Items Efficiently

append():- This function is used to insert the value in its argument to the right end of the deque.

appendleft():- This function is used to insert the value in its argument to the left end of the deque.

Output

deque:  deque([1, 2, 3])

The deque after appending at right is :
deque([1, 2, 3, 4])

The deque after appending at left is :
deque([6, 1, 2, 3, 4])

Refer end for complexity analysis.

Example 2: Popping Items Efficiently

pop():- This function is used to delete an argument from the right end of the deque.

popleft():- This function is used to delete an argument from the left end of the deque.

Output

deque:  deque([6, 1, 2, 3, 4])

The deque after deleting from right is :
deque([6, 1, 2, 3])

The deque after deleting from left is :
deque([1, 2, 3])

Refer end for complexity analysis.

Example 3: Accessing Items in a deque

index(ele, beg, end):- This function returns the first index of the value mentioned in arguments, starting searching from beg till end index.

insert(i, a) :- This function inserts the value mentioned in arguments(a) at index(i) specified in arguments.

remove():- This function removes the first occurrence of the value mentioned in arguments.

count():- This function counts the number of occurrences of value mentioned in arguments.

Output
The number 4 first occurs at a position :
4
The deque after inserting 3 at 5th position is :
deque([1, 2, 3, 3, 3, 4, 2, 4])
The count of 3 in deque is :
3
The deque after deleting first occurrence of 3 is :
deque([1, 2, 3, 3, 4, 2, 4])

Refer end for complexity analysis.

Example 4: Size of a deque

len(dequeue):- Return the current size of the dequeue.

Output

Current Deque:  deque([1, 2, 3, 4, 5, 6])
Size of Deque: 6

The deque after deleting from right is: deque([1, 2, 3, 4, 5])
Size of Deque: 5

Refer end for complexity analysis.

Example 5: Front and Back of a deque

Deque[0] :- We can access the front element of the deque using indexing with de[0].

Deque[-1] :- We can access the back element of the deque using indexing with de[-1].

Output

Current Deque:  deque([1, 2, 3, 4, 5, 6])
Front element of the deque: 1
Back element of the deque: 6

Refer end for complexity analysis.

Example 6: Different operations on deque

extend(iterable):- This function is used to add multiple values at the right end of the deque. The argument passed is iterable.

extendleft(iterable):- This function is used to add multiple values at the left end of the deque. The argument passed is iterable. Order is reversed as a result of left appends.

reverse():- This function is used to reverse the order of deque elements.

rotate():- This function rotates the deque by the number specified in arguments. If the number specified is negative, rotation occurs to the left. Else rotation is to right.

Output

The deque after extending deque at end is :
deque([1, 2, 3, 4, 5, 6])
The deque after extending deque at beginning is :
deque([9, 8, 7, 1, 2, 3, 4, 5, 6])
The deque after rotating deque is :
deque([1, 2, 3, 4, 5, 6, 9, 8, 7])
The deque after reversing deque is :
deque([7, 8, 9, 6, 5, 4, 3, 2, 1])

Refer end for complexity analysis.

Complexity Analysis:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Mar, 2023

# data[].attributes.attachment_link.description
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.