|
5 | 5 |
|
6 | 6 | # Introduction
|
7 | 7 |
|
8 |
| -An [abstract data type (ADT)](https://en.wikipedia.org/wiki/Abstract_data_type) is a mathematical model for data types, typically giving |
| 8 | +Let us begin by explaining the differences between *data types*, *abstract data types* and *data structures*. |
| 9 | + |
| 10 | +## Data Types |
| 11 | + |
| 12 | +[*Data types*](https://en.wikipedia.org/wiki/Data_type) are the most basic classification of data, usually given as |
| 13 | + |
| 14 | +- a set of possible values, |
| 15 | +- a set of allowed operations on them, |
| 16 | +- a concrete representation for computer to manipulate. |
| 17 | + |
| 18 | +An examples is `int` with its [arithmetic operators](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators), represented using 32 bits with the [`Int32 Struct`](https://learn.microsoft.com/en-us/dotnet/api/system.int32?view=net-9.0). |
| 19 | + |
| 20 | +## Abstract Data Types |
| 21 | + |
| 22 | +An [*abstract data type* (ADT)](https://en.wikipedia.org/wiki/Abstract_data_type) is a mathematical model for data types, typically giving |
9 | 23 |
|
10 | 24 | - possible values,
|
11 | 25 | - possible operations on data of this type,
|
12 | 26 | - behavior of those operations.
|
13 | 27 |
|
14 |
| -Data *structures*, on the other hand, are concrete representations of data, from the point of view of a programmer, and not from a user's perspective. |
| 28 | +They are very close to data types, with the following exceptions: |
| 29 | + |
| 30 | +- They exist only conceptually, they have no concrete existence in the context of a language, |
| 31 | +- They define the "contractual agreement" regarding their behavior. |
| 32 | + |
| 33 | +An example is the notion of ["sets"](https://en.wikipedia.org/wiki/Set_(abstract_data_type)), given as a universe, a union operation that returns the union of its elements, a subset predicates that returns "true" if the first argument is a subset of the second, etc. |
| 34 | + |
| 35 | +## Data Structures |
| 36 | + |
| 37 | +[*Data structures*](https://en.wikipedia.org/wiki/Data_structure) are concrete representations of data (comprised of multiple values), from the point of view of a programmer, and not from a user's perspective. |
| 38 | +It is in general given by |
| 39 | + |
| 40 | +- data values, |
| 41 | +- the relationships among them, |
| 42 | +- the functions or operations that can be applied to the data. |
| 43 | + |
| 44 | +They are generally useful for storing and retrieving data, that is, collection of values. |
| 45 | + |
| 46 | +## Summarizing |
15 | 47 |
|
| 48 | +- An abstract data type is the "description", the interface, the contract: this is the most abstract perspective, describing the *behavior* of the structure you want to manipulate. |
| 49 | +- A data type is the implementation of an abstract data type, giving concrete instructions to the computer for how to manipulate it. |
| 50 | +- A data structure is a concrete implementation of how to |
16 | 51 |
|
| 52 | +Some abstract data types are implemented as data types: integers [can be given as an abstract data type](https://cs.stackexchange.com/questions/153597/are-integers-an-abstract-data-type) and are (imperfectly) implemented in C# as `int`^[Typically, `int` cannot correctly add 2,147,483,647 (the value of `int.MaxValue`) and 2, making it an imperfect implementation of integers.] |
| 53 | +Some abstract data types are (imperfectly) implemented as data structures, for example strings of text are implemented in the [`String` class](https://learn.microsoft.com/en-us/dotnet/api/system.string?view=net-9.0). |
17 | 54 |
|
18 | 55 | ## Arrays
|
19 | 56 |
|
20 |
| -Arrays are structures that allow you to store multiple values in memory using a single name and indexes. |
| 57 | +Arrays are data structures that allow you to store multiple values in memory using a single name and indexes. |
21 | 58 | Internally, an array contains a fixed number of variables (called *elements*) of a particular type^[Usually, all the elements of an array have the same type, but an array can store elements of different types if `object` is its type, since any element is actually of type `object`.].
|
22 | 59 | The elements in an array are always stored in a contiguous block of memory, providing fast and efficient access.
|
23 | 60 |
|
|
0 commit comments