Hello fellow programmers,
here I am again with another question.
I'm currently trying to implement a class structure for a Deque with the two functions push_back und push_front. The push_back function is working just fine, but I can't seem to make push_front work.
I've read on the internet, that this is often pictured as a circular container, so I was thinking that push front could maybe move the first pointer to the end of the container if it is at the beginning, but how do I know when it is at the beginning? And what am I supposed to do with the limit at that point?
You would need to keep a separate pointer to the start of the allocated array in order to delete it properly. So you would use that pointer to tell when you are at the beginning.
Your iterators would be more complicated, too, since you can't just use a simple pointer anymore as this won't "wrap around" automatically. You'll need an iterator class with a special increment function.
No, not really. You can probably simplify a few of the implementation details with a total from scratch redo, but the gains would be minor cleanup: you still have to do the things you have to do, and those are going to take some hands-on code writing no matter how you slice it, unless you use the STL building blocks deeper to do the heavy lifting (you already did a fair bit of this) and rewire them to look like a new thing. Even then, you still need it to do what it needs to do, you are just borrowing already written code and splicing around that.
push_front(...) isn't that much different from push_back(...). In fact most of it is the same. Except for line 81. Before *first = x; you need to move all elements in that array using move_backward(...). See: