This is a Python program to implement a dequeue.
The program creates a dequeue and allows the user to perform append and pop operations on it from both sides.
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.
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
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.
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.
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Programming MCQs
- Buy Information Technology Books
- Apply for Programming Internship
- Buy Python Books
- Apply for Python Internship