Classic Data Structures for Embedded Programmers to Learn

1. What is a data structure?

If we want to organize books now, and there are a pile of books, how should we organize them? The simplest one is to put them one by one; or to classify the books, and then subdivide and integrate them according to the major categories; or directly subdivide them into many categories, and there must be many relationships between each category; but have you ever thought about this? Where are the books to be placed? In what environment should it be stored? This requires a different perspective for each sorting method, whether they are all placed on the first floor of a bookshelf, or placed on different shelves, or placed on different bookshelves in the library; the mentioned sorting What the book discusses is what data structures will study. Returning to our programmers, we need to record the sorting methods and storage forms studied above through the computer, because after all, our purpose of sorting out books is to manage and use them conveniently. So in the final analysis, we need to reflect this abstract data and the relationship between the data to the calculation, and implement it in a programming language. Then we can know: data structure studies the relationship between data !

2. Why study data structure?

Study the management mechanism between data in detail, form a data structure model, and use it for reference when encountering actual situations, so that we can apply it to programming faster, design and write better software, and essentially improve the quality of the project!

3. What is data structure?

Everyone should know a classic idea: program = data structure + algorithm;

Then let's talk about the relationship between them, and the points that embedded programmers should pay attention to. We are embedded programmers, we must pay attention to embedded programming thinking, that is, we must pay attention to practicality. Therefore, the focus of learning is the management data thinking of data structure; algorithms are for algorithm engineers to study in depth, and they are research-natured methodologies. We rarely encounter complex algorithm applications. Therefore, it is enough for us to learn the algorithms involved in some classic data structures. If you are interested in the future, you can continue to understand the subject of algorithms in depth; pay attention to where the learning center is at the current stage.

Data structure research from three perspectives: logical relationship, storage relationship, and operational relationship.

The following diagram shows the research relationship of the three perspectives in the data structure:

The logical relationship is an abstract research data relationship, the storage relationship needs to correspond to the physical storage, and the operation relationship is the application operation of the data that needs to be studied after the physical storage is determined;

4. Several classic linear structures:

(1) Sequence table

The picture above shows an empty table that has not yet stored valid data. The storage space is an array, and the last update position of valid data is recorded as an integer last, and the overall table structure is a structure;

The structural code model is as follows:

#define MAXSIZE 10

typedef  int  datatype;

typedef  int  postype;

struct list{

datatype  data[N];

postype last;

};

(2) One-way linked list

The picture above shows a one-way linked list with a head node and new data nodes are constantly added. The storage space is a structure, which needs a data field and a pointer field, the data field stores the data to be stored, and the pointer field records the first address of the next data node;

The structural code model is as follows:

typedef int datatype;

typedef struct node {

        datatype  data;

        struct node *  next;

}Linknode, *Linklist;

(3) Two-way circular linked list

The above figure is a bidirectional circular linked list with a head node and n data nodes. The storage space is a structure. The structure needs to have a data field and two pointer fields. The data field stores the data to be stored. The first pointer field should record the first address of the previous data node, and the second pointer field should Record the first address of the next data node;

The structural code model is as follows:

typedef int datatype;

typedef struct node{

datatype data;

struct node *prior;

struct node *next;

}DLinkNode,  * DLinkList;

(4) Application structure of linear structure:

①Stack--FIFO, LIFO

1) Sequential stack:

The figure above shows a sequential stack with n data storage spaces. The structure is a stack structure. There needs to be a data field and two integer tags in the structure. The data field is the data that needs to be stored in the array. The first tag top records the array subscript of the last data pushed into the stack, and the second A mark len needs to record the size of the entire storage space, which is convenient for knowing the storage capacity of the stack;

The structural code model is as follows:

typedef int datatype;

typedef struct node{

datatype *data;

int maxlen;

int top;

}SeqStack,*SeqStack_t;

2) Chained stack

The above picture shows a chain stack with a head node and n data storage spaces. The structure is a data node. There needs to be a data field and a pointer field in the structure. The data field is the data that needs to be stored in the array. The pointer field should record the first address of the previous stacked node, and the top of the stack is the head node of the linked list. The first data node after the point;

The structural code model is as follows:

typedef int datatype;

typedef struct node{

datatype data;

struct lstacknode * next;

}LinkStacknode, *LinkStacknode_t;

②Queue--first in first out, last in last out

1) sequential queue

The above figure shows a sequential queue with n data storage spaces. It should be noted that this is only an ideal model. In actual use, one storage space needs to be discarded to determine whether an empty table is full or not, and only n-1 storage spaces are used. The structure is a sequential queue. There needs to be an array as the data storage space in the structure. There are two marks. The first mark records the array subscript of the previous position of the data at the head of the queue, and the second mark records the position of the data at the end of the queue. The subscript of the array;

The structural code model is as follows:

#define N 10

typedef int datatype;

typedef struct seqqueue{

datatype data[N];

int front, rear;

}SeqQueue, *SeqQueue_t;

2) Chain queue

The figure above shows a chained queue with a head node and n data storage spaces. The chained queue has two structure models. The data node is the first structure. There needs to be a data field and a pointer field in this structure. The data field is the data that needs to be stored in the array, and the pointer field needs to record the last entry. The first address of the stack team node; the second structure is the queue model, and two pointer fields are required in the structure, the pointer type is the pointer of the first structure type, and a pointer front records the first address of the queue head node, The head of the queue is the first data node after the head node of the linked list, another pointer rear records the head address of the tail node of the queue, and the tail of the queue is the last data node of the linked list.

The structural code model is as follows:

typedef int datatype;

typedef struct node{

datatype data;

struct linkqueuenode *next;

}Queuenode, *Queuenode_t;

typedef struct linkqueue{

linkqueue_pnode front,rear;

}LinkQueue, *LinkQueue_t;

There are other non-linear data models in the data structure, and the space is limited, so I will not list them here, and we will talk about them next time. The above structure has a wide range of applications. If you are interested, you must study and practice!

Friends who are interested in the embedded Internet of Things can learn more about relevant information. (look over)

Guess you like

Origin blog.csdn.net/m0_70911440/article/details/131511919