2

As we know in Java each class that implements Collection interface should implement Iterator<E> iterator() method.

Stack and Queue are parts of collection hierarchy and implement this method as well. So we can write something like this:

Stack stack = new Stack();
//    Deque stack = new ArrayDeque<>();
stack.add( "1" );
stack.add( "2" );
stack.add( "3" );

Iterator i = stack.iterator();
while ( i.hasNext() ) {
  Object o = i.next();
  if ( o.equals( "2" ) ) {
    i.remove();
  }
}

My concerns here are:

  1. Is it ok that we are able to delete elements from the middle of the stack/queue?

  2. Is it ok that stack/queue should "show" only one element (last in stack and first in queue) but actually we are able to get all of them without calling "pop","enqueue" methods?

1
  • 2
    Don't use raw types. Commented Apr 12, 2017 at 8:22

2 Answers 2

4

In the strictest sense, Stack and Queue should not allow iteration over it's element but the only reason this restriction exists is to justify the existence of these data structures. As per sgi

Stack does not allow iteration through its elements.

This restriction is the only reason for stack to exist at all. Note that any Front Insertion Sequence or Back Insertion Sequence can be used as a stack; in the case of vector, for example, the stack operations are the member functions back, push_back, and pop_back. The only reason to use the container adaptor stack instead is to make it clear that you are performing only stack operations, and no other operations.

While some implementations follow this, for example std::stack in C++ does not expose iterator, other implementation provide iterators as a feature. Stack in java is built on top of Vector which implements iterator. Queue is built on top of Collection which requires iterator to be implemented. To answer your questions

Is it ok that we are able to delete elements from the middle of the stack/queue?

Yes. The underlying implementation will make sure that your stack/queue is in a consistent state after the deletion.

Is it ok that stack/queue should "show" only one element (last in stack and first in queue) but actually we are able to get all of them without calling "pop","enqueue" methods?

Yes it's a feature of the underlying implementation.

Sign up to request clarification or add additional context in comments.

Comments

1

Is it ok that we are able to delete elements from the middle of the stack/queue?

Yes since stack/queue both will keep their contract even after deleting , after all the methods exposed by stack/queue manipulate the internal implementation which is used for the deletion

Is it ok that stack/queue should "show" only one element (last in stack and first in queue) but actually we are able to get all of them without calling "pop","enqueu" methods?

Yes you are not violating either contract since the remove is done throw iterator interface of the element not queue or stack

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.