I put integer numbers on the stack in the order I need. How can I extract them by record number?

  • I mean, pop deletes the top entry, but I need a certain one. - St2dat
  • Let massifs be with you) - Bodrov Andrey
  • God forbid you from arrays;) - cy6erGn0m

2 answers 2

The need for random access to the stack probably means the initially incorrect ideology. This almost certainly means that you don't really need a stack. If you need random access and the ability to dynamically add, then use an ArrayList or LinkedList

  • For random access, LinkedList is not suitable. - winger
  • 2
    But it is very suitable to remove arbitrary elements. With frequent deletions from the middle, the ArrayList will bury itself at infinite copying. - cy6erGn0m

Stack - by definition, a data structure that supports only two operations: push (put on top) and pop (remove from top).

If indexing is needed, a data structure is needed that supports indexing;). For example, an array.