If you define push/pop to work on the front of the list you can have an effective stack. One that both push and pop can be implemented in constant time.
Interestingly, as it might come up in an interview, you can do this even if you want to define it as the back. Internally you'll be storing the list in reverse order (or alternately, using a prev field rather than next) The index of get/delete and the order of print, need to be tweaked, but you can get the same result.
That is, whether you say push/pop works on the front or back, you can implement it in constant time.
The doubly-linked list lets you have constant time on both ends.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If you define push/pop to work on the front of the list you can have an effective stack. One that both push and pop can be implemented in constant time.
Interestingly, as it might come up in an interview, you can do this even if you want to define it as the back. Internally you'll be storing the list in reverse order (or alternately, using a
prev
field rather thannext
) The index of get/delete and the order of print, need to be tweaked, but you can get the same result.That is, whether you say push/pop works on the front or back, you can implement it in constant time.
The doubly-linked list lets you have constant time on both ends.