I’m looking for a data structure that is basically a bounded stack.
If I declare that the stack can hold at most 3 items, and I push another item in,
the oldest item is popped.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well a LIFO (Last In First Out) structure is known as a Stack which is what you need for the main part of your requirement
A FIFO (First In First Out) structure is known as a Queue which is what you need for the ability to pop the oldest Items off the back.
A combination of these is known as a Deque. Where you have to the ability to push or pop from either end.
I’m not sure if Java has a built-in Deque datastructure, but if it does (or you can find an implementation on google), You can just put some wrapping logic around to ensure that if you push to the front, and the deque.Count > 3, then also pop from the back.