Container
Overview
A Container is an abstract data type (ADT) that stores and manages a collection of elements. It defines what operations are possible, not how they are implemented.
Containers act as conceptual boxes that organize data and provide controlled access.
What Is a Container?
A container:
- Holds multiple elements
- Defines rules for insertion, removal, and access
- Hides internal representation
Different data structures are concrete implementations of containers.
Core Container Operations
| Operation | Description |
|---|---|
| Insert | Add an element |
| Remove | Delete an element |
| Access | Retrieve elements |
| Search | Find an element |
| Traverse | Visit all elements |
| Size | Number of elements |
How It Works
As an ADT, a container:
- Specifies behavior
- Leaves implementation open
- Can be backed by arrays, lists, trees, or hashes
The same container interface can have many implementations.
Example
Pseudocode
container.add(10)
container.add(20)
container.remove(10)
The container decides how these operations are executed internally.
Real-world Analogy
A container is like a storage box. You care about putting things in and taking things out, not about the internal arrangement 📦.
Time and Space Complexity
- Depends entirely on the underlying implementation.
Use Cases
- Collections frameworks
- Library design
- API abstraction
- Data management layers
Advantages
- Encapsulation
- Implementation flexibility
- Clean interfaces
Limitations
- Abstract, not directly usable
- Performance depends on implementation
- Requires concrete structures
Common Implementations
- List
- Set
- Map
- Stack
- Queue