From the course: Python Data Structures: Stacks, Deques, and Queues
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Node-based queues - Python Tutorial
From the course: Python Data Structures: Stacks, Deques, and Queues
Node-based queues
- [Instructor] In this tutorial we'll be implementing a node-based queue where each element in the queue is represented by a node with a value and a reference to the next node in the queue. So let's start off by defining a class for our node. We can just go ahead and call it Node. And our class will have an init method that initializes the node with a value, and sets the reference to the next node to none because that is currently non-existent. Next, let's go ahead and define a class for our queue. Again, we can just call this Queue. Our Queue class will also have an init method that initializes the last node in the queue. Now let's go ahead and define a method to help us add elements to our queue. This is known as enqueue. The enqueue method creates a new node with a given value and adds it to the end of the queue. If the tail is not none, it updates the next reference of the current tail to point to the new node. The…