How can I create a doubly linked list?
I overall understand the concept of the singly linked list but the doubly is giving me a hard time.
I cannot for the life of me figure how to add a node at the beginning of the list or at the middle. I have looked at examples online but they don't really seem to help. I think I have it figured out for adding at the end of the list. I also am having a hard time linking the previous nodes to one another to create a 'doubly'. I say this because obviously I am doing something wrong because I can display my list forwards correctly but when I try to display it in reverse order it will only show my last element in the list.
Also, I have to make the node in main(). So no, I cannot use the OOP approach. I also cannot use the STL list.
A doubly linked list also has pointers going back the the previous node:
┌───┐ ┌───┐ ┌───┐ ┌───┐
head ──►│ 0 ├──►│ 1 ├──►│ 2 ├──►│ 3 ├──► nullptr
nullptr ◄──┤ │◄──┤ │◄──┤ │◄──┤ │◄── tail
└───┘ └───┘ └───┘ └───┘
To add or remove nodes, work out how to adjust the arrows (pointers).