Data Types, Objects, and Data Structures: Representation and Implementation
A beginner-friendly introduction to data types, objects, data representation, and implementation, with examples in C and C++.
Before learning how arrays, linked lists, stacks, and other data structures work internally, we need to understand three basic concepts: data types, objects, and data representation.
1. What Are Data Types?
A data type tells a programming language what kind of value a variable can store and what operations can be performed on it.
For example:
int age = 20;
float height = 5.10;
char grade = 'A';
Here:
intstores whole numbers.floatstores decimal numbers.charstores a single character.
Each data type has its own memory requirements and supported operations.
For example, adding two integers is valid, while performing operations on characters may have different meanings depending on the programming language.
2. What Are Objects?
An object is a unit that groups related data together. In object-oriented programming, it can also contain functions that work with that data.
For example, consider a student:
- Name: Farhan
- Age: 20
- Roll Number: 101
These values describe one student. Instead of treating them as unrelated pieces of information, we can group them into a single student object.
In C++, this can be done using a structure or a class:
struct Student {
string name;
int age;
int rollNumber;
};
Now, we can create a student:
Student s1 = {"Farhan", 20, 101};
Here, s1 represents one student and contains all three related values.
3. What Is Data Representation?
Data representation refers to how data is stored and organised in computer memory.
A data structure may look simple in our program, but the computer needs a way to arrange its elements in memory.
For example, consider an array:
int marks[4] = {70, 85, 90, 65};
The array stores four integers in consecutive memory locations.
<escape>Contiguous memory</escape> means that the elements are stored next to one another in memory.
A simplified representation:
Index: 0 1 2 3
+------+------+------+------+
Marks: | 70 | 85 | 90 | 65 |
+------+------+------+------+
Each element has an index, which allows us to access it directly.
A linked list, however, stores its elements differently. Each element is kept in a separate unit called a <escape>node</escape>, which contains data and a link to the next node.
+------+-----+ +------+-----+ +------+------+
| 70 | •------->| 85 | •------->| 90 | NULL |
+------+-----+ +------+-----+ +------+------+
Unlike arrays, linked-list nodes do not need to occupy consecutive memory locations.
This shows that different data structures can represent and organise the same data in different ways.
4. What Is Implementation?
Implementation refers to how a data structure is built in a programming language and how its operations are performed.
For example, an array can be implemented using a fixed-size block of memory, while a linked list can be implemented using dynamically created nodes and pointers.
We can also implement operations such as:
- Insertion: Adding a new element.
- Deletion: Removing an element.
- Searching: Finding a particular element.
- Traversal: Visiting elements one by one.
Different data structures implement these operations differently.
5. Representation vs. Implementation
Although these terms are related, they describe different things.
| Representation | Implementation |
|---|---|
| How data is organised in memory. | How the data structure and its operations are built in code. |
| Focuses on the memory arrangement. | Focuses on the programming logic. |
| Example: Array elements stored consecutively. | Example: Writing code to insert or search array elements. |
Data types define what kind of values we work with, objects group related information, and data structures organise data for efficient use.
Understanding how data is represented in memory and how data structures are implemented will help us understand their operations, advantages, and limitations in the upcoming articles.
Originally published at farhanpaal.com — https://farhanpaal.com/articles/data-types-objects-and-data-structures-representation-and-implementation/
Keep a copy
Print this article or save it as a PDF to read offline.
farhanpaal.com