Append and Prepend
In this lesson, you will learn how to implement doubly linked lists and insert elements in it using Python.
We'll cover the following...
In this lesson, we introduce the doubly linked list data structure and code up an implementation of this data structure in Python.
After that, we look at how to append (add elements to the back of the doubly linked list) and prepend (add elements to the front of the doubly linked list).
Finally, we will write a print method to verify that the append and prepend methods work as expected.
As you can see, the doubly linked list is very similar to the singly linked list except for the difference of the previous pointer. In a doubly linked list, a node is made up of the following components:
- Data
- Next (Points to the next node)
- Prev (Points to the previous node)
As illustrated, the prev
of the head node points to NULL while the next
of the tail node also points to NULL.
Let’s go ahead and implement ...