Python Program to Implement Dequeue

This is a Python program to implement a dequeue.

Problem Description

The program creates a dequeue and allows the user to perform append and pop operations on it from both sides.

Problem Solution

1. Create a class Dequeue with instance variable items initialized to an empty list.
2. Define methods append, append_left, pop, pop_left and is_empty inside the class Dequeue.
3. The method append appends data to items from the right.
4. The method append_left appends data to items from the left.
5. The method pop pops from the right from items.
6. The method pop_left pops from the left from items.
7. The method is_empty returns True only if items is empty.

Program/Source Code

Here is the source code of a Python program to implement a dequeue. The program output is shown below.

class Dequeue:
    def __init__(self):
        self.items = []
 
    def is_empty(self):
        return self.items == []
 
    def append(self, data):
        self.items.append(data)
 
    def append_left(self, data):
        self.items.insert(0, data)
 
    def pop(self):
        return self.items.pop()
 
    def pop_left(self):
        return self.items.pop(0)
 
 
q = Dequeue()
print('Menu')
print('append <value>')
print('appendleft <value>')
print('pop')
print('popleft')
print('quit')
 
while True:
    do = input('What would you like to do? ').split()
 
    operation = do[0].strip().lower()
    if operation == 'append':
        q.append(int(do[1]))
    elif operation == 'appendleft':
        q.append_left(int(do[1]))
    elif operation == 'pop':
        if q.is_empty():
            print('Dequeue is empty.')
        else:
            print('Popped value from right: ', q.pop())
    elif operation == 'popleft':
        if q.is_empty():
            print('Dequeue is empty.')
        else:
            print('Popped value from left: ', q.pop_left())
    elif operation == 'quit':
        break
Program Explanation

1. An instance of Dequeue is created.
2. The user is presented with a menu to perform various operations on the dequeue.
3. The chosen operation is performed by calling the corresponding method of the dequeue.

advertisement
advertisement
Runtime Test Cases
Case 1:
Menu
append <value>
appendleft <value>
pop
popleft
quit
What would you like to do? append 3
What would you like to do? append 4
What would you like to do? appendleft 2
What would you like to do? appendleft 1
What would you like to do? pop
Popped value from right:  4
What would you like to do? popleft
Popped value from left:  1
What would you like to do? pop
Popped value from right:  3
What would you like to do? popleft
Popped value from left:  2
What would you like to do? pop
Dequeue is empty.
What would you like to do? quit
 
Case 2:
Menu
append <value>
appendleft <value>
pop
popleft
quit
What would you like to do? append 1
What would you like to do? append 2
What would you like to do? pop
Popped value from right:  2
What would you like to do? pop
Popped value from right:  1
What would you like to do? appendleft 1
What would you like to do? appendleft 2
What would you like to do? pop
Popped value from right:  1
What would you like to do? pop
Popped value from right:  2
What would you like to do? append 1
What would you like to do? append 2
What would you like to do? popleft
Popped value from left:  1
What would you like to do? popleft
Popped value from left:  2
What would you like to do? quit

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.