Table of Contents

Python Deque Implementation

Example Description
Explore the world of Python deques through this comprehensive guide. Discover how to implement essential deque operations like insertion, retrieval, and deletion. Dive into practical examples showcasing effective deque management and gain insights into different use cases for deques.

Introduction:

Python deques (double-ended queues) offer versatile data structure capabilities. In this tutorial, you’ll delve into deque implementation, covering key operations such as insertion, retrieval, and deletion. Through practical examples and explanations, you’ll learn how to create and manage deques effectively, enhancing your Python programming skills.

Code:

def insertFront(myDeque, element):
    myDeque.insert(0, element)

def getFront(myDeque):
    if not isEmpty(myDeque):
      return myDeque[0]
      print("Queue underflow")
      return None

def getRear(myDeque):
    if not isEmpty(myDeque):
      return myDeque[len(myDeque) - 1]
      print("Queue underflow")
      return None

def insertRear(myDeque, element):
    myDeque.append(element)

def isEmpty(myDeque):
    return len(myDeque) == 0

def deletionRear(myDeque):
    if not isEmpty(myDeque):
      return myDeque.pop()
      print("Queue underflow")
      return None

def deletionFront(myDeque):
    if not isEmpty(myDeque):
      return myDeque.pop(0)
      print("Queue underflow")
      return None

def main():
    dQu = list()
    choice = int(input('Enter 1 to use as normal queue, 2 otherwise: '))
    if choice == 1:
        element = input("Data for insertion at rear: ")
        insertRear(dQu, element)
        element = getFront(dQu)
        if element is not None:
          print("Data at the beginning of queue is", element)
        element = input("Data for insertion at rear: ")
        insertRear(dQu, element)
        data = deletionFront(dQu)
        if data is not None:
          print('Data removed from front of queue is', data)
        data = deletionFront(dQu)
        if data is not None:
          print('Data removed from front of queue is', data)
    else:
        element = input("Data for insertion at front: ")
        insertFront(dQu, element)
        element = getRear(dQu)
        if element is not None:
          print("Data at the end of queue is", element)
        element = input("Data for insertion at front: ")
        insertFront(dQu, element)
        data = deletionRear(dQu)
        if data is not None:
          print('Data removed from rear of queue is', data)
        data = deletionRear(dQu)
        if data is not None:
          print('Data removed from rear of queue is', data)

if __name__ == "__main__":
    main()

Logic:

  1. Define functions for deque operations, including insertFront, getFront, getRear, insertRear, isEmpty, deletionRear, and deletionFront.
  2. In the main function:
  3. Initialize an empty dQu list to represent the deque.
  4. Prompt the user to choose between using the deque as a normal queue or a reversed queue.
  5. Depending on the choice:
  6. Insert data at the front or rear of the deque.
  7. Retrieve and print data from the front or rear of the deque.
  8. Delete and print data from the front or rear of the deque.
  9. Call the main function if the script is executed as the main program.

Output:

>>Enter 1 to use as normal queue, 2 otherwise: 1

>>Data for insertion at rear: A

>>Data at the beginning of queue is A

>>Data for insertion at rear: B

>>Data removed from front of queue is A

>>Data removed from front of queue is B