'Is Stack with PushAt/PopAt still a Stack?

Lets say I have a Data Structure similar to Stack but in addition to usual Push/Pop it also has functions such as PushAt/PopAt both of which takes an integer as input and adds/returns the item at that particular location in data structure.

Now Stack is suppose to be LIFO. Does this data structure qualify as "Stack"?



Solution 1:[1]

In HP RPN calculators and in Postscript/PDF, other operators than push and pop exist:

  • swap or exch for permuting top of stack and next element,
  • roll as an extension of swap

Their main data structure is still considered a stack.

pushAt and popAt can be written only with pop/push and roll. So your data structure can still be named stack.

Solution 2:[2]

Technically not. LIFO means (as you know) last-in, first-out. If the last element going in isn't the first to come out, it doesn't satisfy the "semantic interface" (or contract) of a stack.

However, since it seems like you are only adding additional methods and not modify the existing ones your data structure could be used interchangeably with a stack if it is being used like one, i.e. pushAt() and popAt() are never called (for instance because they are "hidden" by passing it as a Stack to a function).

So in that sense your version is a stack in the same way that a list is a stack in Java, for example. It can be used as one, but it can also do other things.

Solution 3:[3]

It's not a stack because it's not LIFO, you have complete control of where the items are get/set it's just a normal list imho.

Solution 4:[4]

Implementation of this would be easy if a linked list were to be used due to how the pointers can be reassigned, although if you were to use an array it would be difficult to pop an entity in the middle and then resize the structure to fill the gap below a complexity of O(n^2). This complexity would be very inefficient with linked list the complexity would only be O(n).

Solution 5:[5]

It might look like an ADT, but it just sounds like an array.

Solution 6:[6]

IMO it's a stack as long as it supports Push and Pop. It doesn't matter if it also supports other actions.

Solution 7:[7]

If its not a LIFO object it can't be qualified as a Stack. I would say its simply a List with Push Pop functionality which again is nothing but AddAtEnd() and RemoveFromEnd()

Solution 8:[8]

Your data structure can be used as a stack.. or as an array/list..
Stack is just a specialized form of a list.. and your implementation appears to negate that specialness.. so I would lean towards calling it a list instead of a stack.

Solution 9:[9]

Actually if you can only Pop and Push elements you can still see it as Stack. A more flexible stack.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mouviciel
Solution 2 n3rd
Solution 3 Lloyd
Solution 4 user1863504
Solution 5 David L Morris
Solution 6 Erich Kitzmueller
Solution 7 SO User
Solution 8 Sam Axe
Solution 9 Nick Dandoulakis