Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
dequeue in data structures and algorithm

Dequeue in Data structure

Posted on 28 May 202023 April 2023 By YASH PAL No Comments on Dequeue in Data structure

In Data Structure Dequeue is known as a Double Ended Queue in which we can insert and delete the data from both ends means we can perform operations enqueue and dequeue from both ends.

Dequeue | Data structures and algorithms

we can implement dequeue using an array, linked list, doubly linked list, and circular linked list. first, let’s see how we can implement dequeue using an array or list in the Python programming language.

Program to implement dequeue using array in Python.

class EmptyQueueError(Exception):
    pass

class Deque:

    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def size(self):
        return len(self.items)

    def insert_front(self, item):
        self.items.insert(0, item)

    def insert_rear(self, item):
        self.items.append(item)

    def delete_front(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.items.pop(0)

    def delete_rear(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.items.pop()

    def first(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.items[0]

    def last(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is Empty")
        return self.items[-1]

    def display(self):
        print(self.items)

#########################################################################
if __name__ == '__main__':
    qu = Deque()

    while True:
        print("1. Insert at the front end")
        print("2. Insert at the rear end")
        print("3. Delete from front end")
        print("4. Delete from rear end")
        print("5. Display first element")
        print("6. Display last element")
        print("7. Display")
        print("8. Size")
        print("9. Quit")

        choice = int(input("Enter your choice : "))

        if choice == 1:
            x = int(input("Enter the element : "))
            qu.insert_front(x)
        elif choice == 2:
             x = int(input("Enter the element : "))
             qu.insert_rear(x)
        elif choice == 3:
            x = qu.delete_front()
            print("Element deleted from front end is ",x)
        elif choice == 4:
            x = qu.delete_rear()
            print("Element deleted from rear end is ",x)
        elif choice == 5:
            print("First element is ",qu.first())
        elif choice == 6:
            print("Last element is ", qu.last())
        elif choice == 7:
            qu.display()
        elif choice == 8:
            print("Size of queue ", qu.size())
        elif choice == 9:
            break        else:
            print("wrong choice ")
        print()

we can also implement dequeue using a doubly-linked list.

Dequeue in Data structures and algorithms

to easily implement of dequeue using the doubly linked list in the data structure. we stored the front and last node’s references in the front and rear variables. so we can easily enqueue and dequeue from both ends.

we already know how to insert and delete a node from the first and last position of a circular linked list. if you don’t know then refer to the articles given below.

Insertion in the doubly linked list
Deletion in the doubly linked list

Program to implement dequeue using a doubly linked list in Python.

class EmptyQueueError(Exception):
    pass
class Node:

    def __init__(self, value):
        self.info = value
        self.prev = None        self.next = None
class Deque:

    def __init__(self):
        self.front = None        self.rear = None
    def is_empty(self):
        return self.front == None
    def size(self):
        count = 0        p = self.front
        while p is not None:
            count +=1            p = p.next
        return count

    def insert_front(self, item):
        temp = Node(item)
        if self.is_empty():
            self.front = self.rear = temp
        else:
            temp.next = self.front
            self.front.prev = temp
            self.front = temp

    def insert_rear(self, item):
        temp = Node(item)
        if self.is_empty():
            self.front = self.rear = temp
        else:
            self.rear.next = temp
            temp.prev = self.rear

        self.rear = temp


    def delete_front(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")

        x = self.front.info
        if self.front.next is None:
            self.front = self.rear = None        else:
            self.front = self.front.next
            self.front.prev = None        return x

    def delete_rear(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")

        x = self.rear.info
        if self.front.next is None:
            self.front = self.rear = None        else:
            self.rear = self.rear.prev
            self.rear.next = None        return x

    def first(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")

        return self.front.info

    def last(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")
        return self.rear.info

    def display(self):
        if self.front is None:
            print("List is empty")
            return        print("List is : ")
        p = self.front
        while p is not None:
            print(p.info, " ", end='')
            p = p.next
        print()

################################
if __name__ == '__main__':
    qu = Deque()

    while True:
        print("1. Insert at the front end")
        print("2. Insert at the rear end")
        print("3. Delete from front end")
        print("4. Delete from rear end")
        print("5. Display first element")
        print("6. Display last element")
        print("7. Display")
        print("8. Size")
        print("9 Quit")

        choice = int(input("Enter your choice : "))

        if choice == 1:
            x = int(input("Enter the element : "))
            qu.insert_front(x)
        elif choice == 2:
            x = int(input("Enter the element : "))
            qu.insert_rear(x)
        elif choice == 3:
            x = qu.delete_rear()
            print("Element deleted from front end is ", x)
        elif choice == 4:
            x = qu.delete_rear()
            print("Element deleted from rear end is ", x)
        elif choice == 5:
            print("Last element is ", qu.first())
        elif choice == 6:
            print("Last element is ", qu.last())
        elif choice == 7:
            qu.display()
        elif choice == 8:
            print("size of queue ", qu.size())
        elif choice == 9:
            break        else:
            print("Wrong choice ")
        print()

Program to implement dequeue using a doubly linked list in Python. 

class EmptyQueueError(Exception):
    pass
class Deque:

    def __init__(self, default_size=10):
        self.items = [None] * default_size
        self.front = 0        self.count = 0
    def is_empty(self):
        return self.count == 0
    def size(self):
        return self.count

    def insert_front(self, item):
        if self.count == len(self.items):
            self.resize(2*len(self.items))

        i = (self.front + self.count) % len(self.items)
        self.items[i] = item
        self.count += 1
    def insert_rear(self, item):
        if self.count == len(self.items):
            self.resize( 2 * len(self.items))

        i = (self.front + self.count) % len(self.items)
        self.items[i] = item
        self.count +=1
    def delete_front(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")

        x = self.items[self.front]
        self.items[self.front] = None        self.front = (self.front + 1) % len(self.items)
        self.count -= 1        return x

    def delete_rear(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")
        rear = (self.front + self.count - 1) % len(self.items)
        x = self.items[rear]
        self.items[rear] = None        self.count -=1        return x

    def first(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")
        return self.items[self.front]

    def last(self):
        if self.is_empty():
            raise EmptyQueueError("Queue is empty")
        rear = (self.front + self.count - 1) % len(self.items)
        return self.items[rear]

    def display(self):
        print(self.items)

    def resize(self,newsize):
        old_list = self.items
        self.items = [None] * newsize
        i = self.front
        for j in range(self.count):
            self.items[j] = old_list[i]
            i = (1+i)%len(old_list)
        self.front = 0

######################################################################3
if __name__ == '__main__':
    qu = Deque(6)

    while True:
        print("1. Insert at the front end")
        print("2. Insert at the rear end")
        print("3. Delete from front end")
        print("4. Delete from rear end")
        print("5. Display first element")
        print("6. Display last element")
        print("7. Display")
        print("8. Size")
        print("9. Quit")

        choice = int(input("Enter your choice : "))

        if choice == 1:
            x = int(input("Enter the element : "))
            qu.insert_front(x)
        elif choice == 2:
            x = int(input("Enter the element : "))
            qu.insert_rear(x)
        elif choice == 3:
            x = qu.delete_front()
            print("Element deleted from front end is ", x)
        elif choice == 4:
            x = qu.delete_rear()
            print("Element deleted from rear end is ", x)
        elif choice == 5:
            print("First element is ", qu.first())
        elif choice == 6:
            print("Last element is ", qu.last())
        elif choice == 7:
            qu.display()
        elif choice == 8:
            print("Size of queue ", qu.size())
        elif choice == 9:
            break        else:
            print("Wrong choice")
        print()
Computer Science Tutorials, Data Structures Tutorials Tags:computer science, Data Structure

Post navigation

Previous Post: Circular queue in Data structure
Next Post: Priority queue in Data structure

Related Tutorials

Reading input in c programming Reading Input in a C program C Programming Tutorials
The First C Program C Programming Tutorials
Compiling C Programs C Programming Tutorials
History of c programming language HISTORY OF C Programming Language C Programming Tutorials
c character sets C Character Sets C Programming Tutorials
c programming interview questions and answers C Programming Interview Questions and Answers C Programming Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

  • Algorithms Tutorials
  • Basic Programming
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the...

    Learn More “Java Interview Questions and Answers (2023)” »

  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet,...

    Learn More “IoT in Healthcare” »

  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis...

    Learn More “IoT for Industry” »

  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges...

    Learn More “IoT in Agriculture” »

  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle...

    Learn More “IoT in Logistics and Supply Chain” »

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme