I'm having trouble with the terminology. It feels as if people use the term interchangeably and/or those terms overlap each other. The funny thing is, I can't explain the difference between those two terms myself.
I did some online research to find out the internationally accepted definition of those two terms; which might not exist.
Data structure
What's a data structure?
a data structure is a data organization and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data, i.e., it is an algebraic structure about data.
Data structures serve as the basis for abstract data types (ADT).
https://en.wikipedia.org/wiki/Data_structure
Now let's find some examples of data structures. I found:
- All kinds of array's, including an Array and Associative Array as a data structure.
- All kinds of list's, e.g. linked list, doubly linked list.
- All kinds of trees, e.g. binary search tree.
- All kinds of heaps, including an Heap data structure.
- All kinds of hash-based structures, e.g. hash table.
- All kinds of graphs, including a Graph data structure.
https://en.wikipedia.org/wiki/List_of_data_structures
Abstract data type
What's an abstract data type?
In computer science, an abstract data type (ADT) is a mathematical model for data types, defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations. This mathematical model contrasts with data structures, which are concrete representations of data, and are the point of view of an implementer, not a user.
https://en.wikipedia.org/wiki/Abstract_data_type
Now let's find some examples. Here are some:
- List
- Map -> Associative array
- Graph
- Tree
https://en.wikipedia.org/wiki/Abstract_data_type#Common_ADTs
My opiniated view
As a backend developer with php experience my opiniated definition of an Abstract Data Type is like an interface or abstract class with abstract methods. It defines the operations. A Data Structure is like an implementation of that abstract data type.
A Stack is an Abstract Data Type with the following operations:
- push
- pop
In php it's possible to implement a Stack as the following:
class Stack {
private array $data = [];
public function push(string|int $data): void
{
array_push($this->data, $data);
}
public function pop(): string|int|null
{
return array_pop($this->data);
}
}
$stack = new Stack();
$stack->push(1);
$stack->push(2);
$stack->push(3);
$intA = $stack->pop();
$intB = $stack->pop();
$intC = $stack->pop();
$null = $stack->pop();
var_dump($intA, $intB, $intC, $null); // int(3), int(2), int(1), NULL
The Stack class here is a data structure that abides to the laws of the Stack Abstract Data Type (it has push and pop operation).
The questions
So a List, Map, Graph, Tree are both: a data structure and an abstract data type?
Is my opiniated definition more or less correct?
Can you be in a situation in which you can see "hey look, you see this? This is an abstract data type not a data structure" or "this is not a data structure because of x and y".
Is an Abstract Data Type just a "design pattern"?
Can you clarify this brain fog for me?