Skip to Content
Python Data Structures and Algorithms
book

Python Data Structures and Algorithms

by Benjamin Baka
May 2017
Intermediate to advanced
310 pages
8h 5m
English
Packt Publishing
Content preview from Python Data Structures and Algorithms

Enqueue operation

The enqueue operation or method uses the insert method of the list class to insert items (or data) at the front of the list:

    def enqueue(self, data):         self.items.insert(0, data)         self.size += 1 

Do note how we implement insertions to the end of the queue. Index 0 is the first position in any list or array. However, in our implementation of a queue using a Python list, the array index 0 is the only place where new data elements are inserted into the queue. The insert operation will shift existing data elements in the list by one position up and then insert the new data in the space created at index 0. The following figure visualizes this process:

To make our queue reflect the addition of the new element, the size is increased ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Structures and Algorithms in Python

Data Structures and Algorithms in Python

Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Hands-On Data Structures and Algorithms with Python - Second Edition

Hands-On Data Structures and Algorithms with Python - Second Edition

Dr. Basant Agarwal, Benjamin Baka, David Julian
Data Structures & Algorithms in Python

Data Structures & Algorithms in Python

John Canning, Alan Broder, Robert Lafore

Publisher Resources

ISBN: 9781786467355Supplemental Content