Dequeue in Data Structures

We See Queues in our daily life. We see people standing in queues at railways counters, ATM counters, super market bill counters, cinema ticket counters etc. Queue follows one mechanism called FIFO (First In First Out). In same way, we can arrange the elements in memory in such a way that the first element will come first, second element will come second and the last element will come last. Since It follows FIFO rule we can't insert a data items or elements in middle or starting position. insertions should be takes place in only at the rear of the queue, similarly the deletion operation can be performed only at the Front of the queue neither from Rear from Middle. We can search an element from a Queue using the position i.e., using the index.


Python Program to create and use deque

from collections import deque
dq=deque()
choice=0
def display():
    print("="*50,"\n",' '*15,'|',dq,'|',' '*15,'\n',"="*50,'\n')
print("Initially the Deque is: ",[])
while True:
    print("="*15,"Deque Operations","="*15)
    print("1. Add an element at the front")
    print("2. Add an element at the raer")
    print("3. Search an Element")    
    print("4. Delete an element at the front") 
    print("5. Delete an element at the rear")
    print("6. Remove an element from the middle")
    print("7. Exit")    
    choice = int(input("Enter your choice: "))
    if choice==1:
        element = input("Enter element: ")
        dq.appendleft(element)
        display()
    elif choice==2:
        element=input("Enter an element: ")
        dq.append(element)
        display()
    elif choice == 3:
        element = input("Enter an element: ")
        c = dq.count(element)
        display()
        print("Number of times the element found is: ", c)
    elif choice==4:
        if len(dq) == 0:
            print("Deque is empty!")
            display()
        else:
            dq.popleft()            
            display()
    elif choice==5:
        if len(dq) == 0:
            print("Deque is empty!")
            display()
        else:
            dq.pop()
            display()
    elif choice == 6:
        element=input("Enter an element: ")
        try:
            dq.remove(element)
            display()
        except ValueError:
            display()
            print("Element not found!")
    elif choice == 7:
        break
    else:
        print("Invalid Input! please enter the correct choice.(1-7)")

OUTPUT:

Initially the Deque is: []
=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 1
Enter element: AI
================================================== 
                 | deque(['AI']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 2
Enter an element: Software
================================================== 
                 | deque(['AI', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 1
Enter element: Silan
================================================== 
                 | deque(['Silan', 'AI', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 2
Enter an element: Software
================================================== 
                 | deque(['Silan', 'AI', 'Software', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 3
Enter an element: Software
================================================== 
                 | deque(['Silan', 'AI', 'Software', 'Software']) |                 
 ================================================== 

Number of times the element found is:  2
=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 1
Enter element: Silan
================================================== 
                 | deque(['Silan', 'Silan', 'AI', 'Software', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 4
================================================== 
                 | deque(['Silan', 'AI', 'Software', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 5
================================================== 
                 | deque(['Silan', 'AI', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 6
Enter an element: AI
================================================== 
                 | deque(['Silan', 'Software']) |                 
 ================================================== 

=============== Deque Operations ===============
1. Add an element at the front
2. Add an element at the raer
3. Search an Element
4. Delete an element at the front
5. Delete an element at the rear
6. Remove an element from the middle
7. Exit
Enter your choice: 7

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext