Basic Concepts of Data Structures
Before formally studying various data structures, you need to master how they are classified, the concept of abstract data types, and review the core C-language knowledge points closely related to data structures.
Classification of data structures
Data structures can be classified along two dimensions: logical structure and storage method. Understanding these two classification methods helps you quickly determine which type of data structure to choose when facing a specific problem.
The figure above shows the complete classification system of data structures; each is explained below.
Classification by logical structure
The logical structure describes the abstract relationships between data elements, without involving the actual storage of data in memory.
| Type | Element relationship | Typical examples | Applicable scenarios |
|---|---|---|---|
| Linear structure | One-to-one sequential relationship | Arrays, linked lists, stacks, queues | Data has an obvious sequential order, such as a to-do list or browsing history |
| Tree structure | One-to-many hierarchical relationship | Binary trees, BST, heaps | Data has hierarchical belonging, such as a file system directory or an organization chart |
| Graph structure | Many-to-many network relationship | Directed graphs, undirected graphs, weighted graphs | Data relationships are complex, such as a social network or map navigation |
Classification by storage method
The storage method describes the actual organization of data in memory.
| Type | Feature | Typical examples | Trade-off |
|---|---|---|---|
| Sequential storage | Elements are stored consecutively in contiguous memory space | Arrays, stacks/queues implemented with arrays | Fast access (O(1)), but slow insertion/deletion, fixed size |
| Linked storage | Elements can be scattered and connected via pointers | Linked lists, trees, graphs (adjacency list) | Fast insertion/deletion, but no random access, extra pointer overhead |
| Indexed storage | Build an additional index table to locate data | Hash tables (indexed via a hash function) | Extremely fast lookup, but requires extra space to store the index structure |
The same logical structure can be implemented with different storage methods. For example, a stack (logically a linear structure) can be implemented with an array (sequential storage) or with a linked list (linked storage). Which implementation to choose depends on the specific application scenario's differing requirements for access speed, insertion/deletion efficiency, and memory usage.
Abstract Data Type (ADT)
Abstract Data Type (ADT for short) is a core concept in learning data structures.
It is an abstract description of a set of data and a set of operations defined on that data; it only cares about "what to do," not "how to do it."
Understand ADT through the example of a "stack":
| ADT level (what to do) | Implementation level (how to do it) |
|---|---|
| Define operations: push (push onto stack), pop (pop from stack), peek (view the top of the stack) | Implement with an array: maintain an array + a top pointer |
| Define behavior: last-in-first-out (LIFO) | Implement with a linked list: use the head of the list as the top of the stack |
| Specify constraints: operations are only allowed from one end | Both implementations satisfy the ADT definition, but have different performance characteristics |
This idea of "separating interface from implementation" is the cornerstone of modular design in modern software engineering.
When you use a stack, you only need to know how to use operations like push and pop, without caring whether it is implemented with an array or a linked list - this is exactly the value of the ADT.
C language foundational knowledge review
Before learning data structures, you need to master the following three core C-language knowledge points, which run through almost all chapters of this tutorial.
Pointers
A pointer is one of the most important and flexible features of C; it stores the memory address of another variable, through which the target variable can be indirectly accessed and manipulated.
Example
#include
int
main
(
)
{
int
num
=
42
;
/* Ordinary integer variable */
int
ptr
=
&
num
;
/* ptr is a pointer to int, storing the address of num */
printf
(
"num value: %d\n"
,
num
)
;
/* Output: 42 */
printf
(
"num address: %p\n"
,
&
num
)
;
/* Output: 0x16d2f2f38 (example address) */
printf
(
"ptr stored address: %p\n"
,
ptr
)
;
/* Output: same as above */
printf
(
"*ptr dereferenced value: %d\n"
,
ptr
)
;
/* Output: 42, * is the dereference operator */
ptr
=
100
;
/* Modify the value of num through the pointer */
printf
(
"After modification num = %d\n"
,
num
)
;
/* Output: 100 */
return
0
;
}
Core uses of pointers in data structures:
| Purpose | Example |
|---|---|
| Build connections between dynamic nodes | The next pointer in a linked list node, the left/right pointers in a tree node |
| Indirectly access and modify data | Modify the value of an external variable through a pointer parameter in a function |
| Dynamic memory management | malloc returns the starting address of the allocated memory and must be received with a pointer |
| Avoid large amounts of data copying | Passing a pointer is more efficient than passing by value when a function passes a large structure |
Beginners are most easily confused by
*and&'s usage: when declaring,int *pindicates p is a pointer type; when using,*pindicates dereferencing (taking the value);&xindicates taking the address. These are two completely different operators; understanding this distinction is a prerequisite for using pointers well.
struct
A struct allows combining multiple data of different types into a custom type; it is the basic unit for building composite data structures such as linked list nodes and tree nodes.
Example
#include
#include
/* Define a linked list node struct */
struct
Node
{
int
data
;
/* Data field: stores the actual data */
struct
Node
next
;
/* Pointer field: points to the next node */
}
;
/* Define a student information struct */
struct
Student
{
int
id
;
/* Student ID */
char
name
[
50
]
;
/* Name, character array */
float
score
;
/* Score */
}
;
int
main
(
)
{
/* Create a node */
struct
Node node1
;
node1.
data
=
10
;
node1.
next
=
NULL
;
/* NULL means there is no subsequent node at present */
printf
(
"node1.data = %d\n"
,
node1.
data
)
;
/* Output: 10 */
/* Create a student record */
struct
Student stu
;
stu.
id
=
1001
;
strcpy
(
stu.
name
,
"RUNOOB"
)
;
/* strcpy: copy a string into a character array */
stu.
score
=
95.5
;
printf
(
"Student ID: %d, Name: %s, Score: %.1f\n"
,
stu.
id
,
stu.
name
,
stu.
score
)
;
/* Output: Student ID: 1001, Name: RUNOOB, Score: 95.5 */
return
0
;
}
Structs combined with pointers enable flexible dynamic data structures. For example, the self-referential structure of a linked list node (struct Node* next pointing to the next node of the same type) is exactly the foundation of linked list implementation.
Dynamic memory allocation
Dynamic memory allocation allows a program to request and release heap memory at runtime as needed; it is the key to building variable-size data structures (such as linked lists and trees).
The C language provides four core functions:
| function | Function | Example |
|---|---|---|
| malloc | Allocate a specified number of bytes of memory, content uninitialized | int* p = malloc(10 * sizeof(int)); |
| calloc | Allocate memory and initialize it to zero | int* p = calloc(10, sizeof(int)); |
| realloc | Resize already allocated memory | p = realloc(p, 20 * sizeof(int)); |
| free | Free the allocated memory, returning it to the system | free(p); |
Example
#include
#include
int
main
(
)
{
int
n
=
5
;
/* malloc: allocate contiguous space for n ints on the heap */
int
arr
=
(
int
)
malloc
(
n
sizeof
(
int
)
)
;
/* Check whether allocation succeeded; malloc returns NULL on failure */
if
(
arr
==
NULL
)
{
printf
(
"Memory allocation failed!\n"
)
;
return
1
;
/* Abnormal exit */
}
/* Use dynamically allocated memory like a normal array */
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
arr
[
i
]
=
(
i
1
)
10
;
/* Assignment: 10, 20, 30, 40, 50 */
}
printf
(
"Dynamic array contents: "
)
;
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
printf
(
"%d "
,
arr
[
i
]
)
;
}
printf
(
"\n"
)
;
/* Output: Dynamic array contents: 10 20 30 40 50 */
free
(
arr
)
;
/* Free the memory to prevent memory leaks */
arr
=
NULL
;
/* Good habit: set the pointer to NULL to prevent dangling pointers */
return
0
;
}
The C language does not automatically reclaim dynamically allocated memory. Every
malloc(orcalloc) must have a correspondingfree, otherwise it will cause a memory leak. In real projects, memory leaks gradually exhaust system resources and eventually cause the program to crash. This is one of the most common mistakes made by C beginners.