The short answer
A stack is both, depending on what you are describing. As an abstract data type (ADT), a stack is defined by its operations and its last-in, first-out rule. As a data structure, a stack is a specific way of storing its items, such as an array or a linked list. The ADT is the promise, and the data structure is how the promise is kept.
What the stack ADT defines
The core stack operations in data structure courses are usually these four:
push(x): add an item to the toppop(): remove and return the top itemtop()orpeek(): look at the top item without removing itis_empty(): check whether there are any items
Items leave in the reverse of the order they arrived, which is the last-in, first-out (LIFO) rule. Popping an empty stack is an error case, and a good definition says what happens then. Many implementations raise an exception.
Nothing in that list says how the items are stored. That is what keeps it an ADT.
A stack as an array-based data structure
Here is one concrete version in Python, using a list as the storage:
class Stack:
def __init__(self):
self._items = []
def push(self, item):
self._items.append(item)
def pop(self):
if self.is_empty():
raise IndexError("pop from empty stack")
return self._items.pop()
def top(self):
if self.is_empty():
raise IndexError("top from empty stack")
return self._items[-1]
def is_empty(self):
return len(self._items) == 0
A Python list works like a resizable array. Appending to and popping from the end are typically constant time on average, which is why the end of the list is the natural place for the top of a stack.
Calling code only sees the four methods:
s = Stack()
s.push(1)
s.push(2)
print(s.pop()) # 2
print(s.top()) # 1
A stack as a linked-list data structure
A linked stack stores each item in a node. Each node holds a value and a pointer to the node below it, and the stack keeps a pointer to the top node.
- Push creates a new node, points it at the current top, and makes it the new top.
- Pop takes the top node’s value, moves the top pointer down one node, and returns the value.
Both operations touch only the top node, so both are constant time. The linked version never needs to resize, but each node spends extra memory on its pointer.
The push, pop, top and is_empty operations keep the same names in both versions, so code that uses the stack doesn’t have to change.
How languages provide stacks
Languages often ship a stack, which is an implementation of the ADT:
- C++:
std::stackis a container adapter. By default it usesstd::dequeas its underlying container, and you can choosestd::vectororstd::listinstead. Itspop()returns nothing, so read the top element withtop()first. - Java: the
Stackclass extendsVector. Its documentation recommends theDequeinterface, and implementations such asArrayDeque, for new code. - Python: a list already behaves like a stack, as shown above, and
collections.dequeis another common choice.
Each of these is a data structure chosen to support the stack ADT.
Why the label matters
In a course, a question about a stack’s definition is usually asking about the ADT. A question about efficiency, memory or implementation is usually asking about the data structure. Using both terms correctly signals that you know the difference.
Stacks show up in many places, including undo history, checking balanced brackets and evaluating expressions. Readers who ask what are the 10 applications of stack will find that the longer list overlaps with these.
Don’t mix it up with other stacks
Two other things share the name:
- The call stack: the runtime structure that tracks active function calls. The language runtime manages it. It is not the stack ADT you write in your own code.
- The stack memory region: part of a program’s memory layout. The difference between stack and heap memory is about where variables live, not about the ADT.
When a question says “stack,” check whether it means the ADT, a data structure, or one of these runtime ideas.
Summary
- A stack is an ADT defined by push, pop, top and is_empty, with last-in, first-out behavior.
- A stack data structure is one concrete way to store those items, such as an array or a linked list.
- Languages supply stacks in different forms, and the underlying structure affects cost.
- If an exam or interview asks “ADT or data structure?”, it helps to answer both.