17-4. Deque

A deque, or double-ended queue, resembles a queue in that it's an ordered assortment of items. However, it differs in that it features two ends: a front and a rear, where items maintain their position. This structure allows for the addition of new items at either end and the removal of existing items from either end as well.

  • How it works?

  • Deque Implementation

Test Program

Output:

Is empty? True

True
hawk
10
tiger

tiger
10
hawk
True

 

  • Deque Applications

Example: Palindrome

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.

To determine if a given string is a palindrome, we can create a deque object with the string. Then, we can check whether the elements at the front and the rear of the deque match each other. If they consistently match as you remove elements from both ends, the string is a palindrome.

 

Output:

True

False

True

False