Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learning Functional Data Structures and Algorithms

You're reading from   Learning Functional Data Structures and Algorithms Learn functional data structures and algorithms for your applications and bring their benefits to your work now

Arrow left icon
Product type Paperback
Published in Feb 2017
Publisher Packt
ISBN-13 9781785888731
Length 318 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
 S. Khot S. Khot
Author Profile Icon S. Khot
S. Khot
 Mishra Mishra
Author Profile Icon Mishra
Mishra
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Learning Functional Data Structures and Algorithms
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. Why Functional Programming? FREE CHAPTER 2. Building Blocks 3. Lists 4. Binary Trees 5. More List Algorithms 6. Graph Algorithms 7. Random Access Lists 8. Queues 9. Streams, Laziness, and Algorithms 10. Being Lazy - Queues and Deques 11. Red-Black Trees 12. Binomial Heaps 13. Sorting

Chapter 10.  Being Lazy - Queues and Deques

In Chapter 8, Queues we looked at functional queues. Deques are a kind of enhanced queues, allowing you to perform insertion and deletion at both the the front and rear ends of the queues.

Why would we need to insert and delete elements from both ends? Well, this is how we would check whether a string is a palindrome. A palindrome is a string that reads the same when read backwards, that is, s == s.reverse always holds for palindromes. For example, MADAM is a palindrome. (See http://www.fun-with-words.com/palin_example.html for more fun examples of palindromes.)

The following figure shows a string and how we could use deletion at both ends to check whether it is a palindrome:

The algorithm is pretty simple. We treat the string as a deque of characters. We keep removing successive elements from both the front and rear ends of the string and compare them. If we find a mismatch, the string is not a palindrome.

Another example is how we would model...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime
Visually different images