views:

516

answers:

6

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.

+1  A: 

You need a queue. A singly linked list that records the first and last items. A doubly linked one if you would like to change from O(n) to O(1) traversal to update the last item.

You push objects at the front of the queue. And if the length is greater than 3, you pop the back.

Unknown
Given a constant bound, e.g. 3, to the number of items in the queue at any time, every operation is O(1), since for a constant c O(c) = O(1).
Christoph
@ Christoph: yes if the stack is limited to 3. But the author didn't say the length would always be 3. If he wanted a stack of length 1000, it would be O(1000) pointer traversals to get to the new end of the list with a singly linked list. With a doubly linked list, you record the last element and traverse backwards only once to get the new "last element".
Unknown
The size of the queue would not exceed 10 but should be variable.
Jacques René Mesrine
+2  A: 

You'll be able to implement this using a wrapper over a deque (http://en.wikipedia.org/wiki/Deque), or double-ended queue. Just make sure to call the pollLast method inside the offerFirst method if the stack size is reached.

sykora
+1  A: 

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.

Eoin Campbell
Deque is a standard interface in Java from 6 on. Implementations are LinkedList<E>, ArrayDeque<E>, and LinkedBlockingDeque<E>. As you may have noticed, LinkedList was retrofitted with this interface.
R. Bemrose
Side note: Deque classes are recommended in Java 6 over Stack and Stack will most likely be deprecated in Java 7.
R. Bemrose
A: 

This is in C# as I don't know Java I'm afraid but the idea should translate.

public class BoundedStack<T>
{
    private readonly int limit;
    private LinkedList<T> list;

    public BoundedStack(int limit)
    {
        this.limit = limit;
        list = new LinkedList<T>();
    }

    public void Push(T item)
    {
        if (list.Count == limit) list.RemoveFirst();
        list.AddLast(item);
    }

    public T Pop()
    {
        if (list.Count == 0) 
            throw new IndexOutOfRangeException("No items on the stack");

        var item = list.Last.Value;
        list.RemoveLast();

        return item;
    }

    public int Count()
    {
        return list.Count;
    }
}
Garry Shutler
A: 

Apache commons has something close to what you need except it is Fifo: CircularFifoBuffer. I think you will be stuck writing a custom wrapper to make a Lifo like implementation.

Alex B
+2  A: 

I'd write my own Deque implementation based on a ring buffer.

erickson