We learned how to implement singly linked list in class. And our professor kind of mentioned us doing doubly linked list, but apparently its so easy that he really didn’t explain how to do it in full detail. I’m really good at with working with singly linked list, but could someone tell me how to make a doubly linked list?
Share
If you already have a sound definition of a singly linked list, then a doubly linked list is easy.
Before you probably had something like
This will need to change to
Now you can basically do your operations in reverse by using previous instead of next when traversing the list.
Keep in mind you need to be careful with your ‘book keeping’ to keep things pointed at the right things when adding or removing.
I always tell students to draw every step of adding and removing from a linked list when writing functions. And to always make sure that you keep a reference to everything in your drawing via a pointer somewhere until it is deleted.