# Start from scratch embedded system development learning Day8 (data structure)

Posted by nickk on Sat, 12 Feb 2022 14:06:51 +0100

# 1, Curriculum system

Concept

Sequence table, single chain table, one-way circular table, two-way circular table, queue, stack

Tree

Figure

Algorithm:

Search algorithm

# 2, Why learn data structures

1. Program = data structure + algorithm

Data structure is not only a very important thing to write code, but also a basic course

2. Data structure is a very important part of any programming language

For example, STL (Standard Template Library) in c + +

The essence of database is to write the content of data structure

The traversal algorithm of data graph is the basis of artificial intelligence

Red and black trees will be reflected in the drive

# 3, Concept of data structure

## 3.1 basic concepts

Data structure:

Data: research object

Structure: relationship between data

Data structure is mainly to study the relationship between data and data

In actual development, data structure is mainly used to temporarily store data in memory after understanding the relationship between data

## 3.2 definition of data structure

Data structure mainly studies the logical relationship, storage relationship and operation of data

## 3.3 logical relationship

Logical relationship mainly refers to the logical relationship between data, mainly refers to the collar relationship

Logical relationship involves the concepts of direct predecessor and direct successor when considering the relationship between data

Linear relationship: one-to-one relationship. Any data can only have one direct precursor and one direct successor

For example: linear table, stack, queue

Tree relationship: one to many relationship. Any data can only have one direct precursor, but can have multiple direct successors

For example: tree, binary tree

Graphical relationship (mesh relationship): many to many relationship. Any data has multiple direct precursors and multiple direct successors

For example: Figure

## 3.4 storage relationship

Storage relation refers to how data is stored in memory

Sequential storage:

Data in memory will open up a continuous memory space for storage. Generally, arrays are used to store data

Chained storage:

When data is stored in memory, it is not necessary to open up a continuous space.

Index storage (generally not used)

Hash storage (generally not used)

Note: in theory, any logical structure can be implemented in two storage methods.

# 4, Sequential table (sequential storage of linear table)

## 4.1 concept

Linear table: a one-to-one relationship between data and data

Sequential storage: it is necessary to open up a continuous memory space in memory to store data. Generally, arrays are used to store data. In order to facilitate the operation of arrays, a variable is usually defined to save the subscript of the last element

## 4.2 operation of sequence table

### 4.2.1 create an empty sequence table

```#include <stdio.h>
#include <stdlib.h>

//In order to improve the expansibility of the code, alias the data type to facilitate the modification of the data in the table
typedef int DataType;
#define N 32
//Define a structure
typedef struct
{
DataType data[N];
int pos;    //Array subscript
}seqlist;
//Creation of sequence table
seqlist *SeqlistCreate();
int main()
{
seqlist *st = SeqlistCreate();
return 0;
}
//Creation of sequence table
seqlist *SeqlistCreate()
{
//Apply for space in the stacking area
seqlist *st = (seqlist *)malloc(sizeof(seqlist));
//Initialization to identify that there are no elements in the current sequence table
st->pos = -1;
//Returns the first address of the sequence table
return st;
}```

### 4.2.2 judge whether the sequence table is full

```//Judge whether the sequence table is full
int SeqlistFull(seqlist *st)
{
//Method 1
#if 0
if(st->pos == N - 1)
{
return 1;
}
else
{
return 0;
}
#endif
//Method 2
return st->pos == N - 1 ? 1 : 0;
}```

### 4.2.3 insert data

```//insert data
void SeqlistInsert(seqlist *st,DataType value)
{
//Judge whether the sequence table is full
if(SeqlistFull)
{
printf("The sequence table is full and cannot be inserted!\n");
return;
}
else
{
//Save the last element variable pos auto increment
st->pos++;
//Insert data into pos location
st->data[st->pos] = value;
printf("Successfully inserted!\n");
}
}```

### 4.2.4 traversal sequence table

```//Traversal sequence table
void SeqlistPrint(seqlist *st)
{
int i;
for(i = 0;i <= st->pos;i++)
{
printf("%d",st->data[i]);
}
putchar(10);
}```

### 4.2.5 judge whether the sequence table is empty

```//Judge whether the sequence table is empty
int SeqlistNull(seqlist *st)
{
//There are two ways to judge whether it is full or not
return st->pos == -1 ? 1 : 0;
}```

### 4.2.6 delete data and return deleted data

```//Delete data and return deleted data
DataType SeqlistDelete(seqlist *st)
{
if(SeqlistNull)
{
printf("The sequence table is empty and cannot be deleted!\n");
return (DataType) - 1;
}
else
{
DataType value = st->data[st->pos];
st->pos--;
printf("Delete succeeded!\n");
return value;
}
}```

### 4.2.7 insert data by location

```//Insert data by location
void SeqlistInsertByPos(seqlist *st,int p,DataType value)
{
if(SeqlistFull(st))
{
printf("The sequence table is full and cannot be inserted!\n");
return;
}
//Determine whether the position can be inserted
else if (p < 0 || p > st->pos +1)
{
printf("Incorrect position, failed to insert!\n");
return;
}
else
{
int i;
//If the last bit is inserted, insert it directly
if(p = st->pos + 1)
{
st->data[p] = value;
st->pos++;
}
else
{
//The data after the insertion point is moved back one bit
for ( i = st->pos; i > p; i--)
{
st->data[i + 1] = st->data[i];
}
//insert data
st->data[p] = value;
st->pos++;
}
printf("Successfully inserted!\n");
return;
}
}
```

### 4.2.8 delete the data according to the location and return the deleted data

```//Delete the data according to the location and return the deleted data
DataType SeqlistDeleteByPos(seqlist *st,int p)
{
if(SeqlistNull(st))
{
printf("The sequence table is empty and cannot be deleted!\n");
return (DataType) - 1;
}
//Judge whether the location can be deleted
else if (p < 0 || p > st->pos +1)
{
printf("Incorrect location, deletion failed!\n");
return (DataType) - 1;
}
else
{
//Save the data to be deleted in value
DataType value = st->data[p];
int i;
//Move the data after the deletion position forward one bit
for(i = p; i < st->pos;i++)
{
st->data[i] = st->data[i + 1];
}
st->pos--;
printf("Delete succeeded!\n");
return value;
}
}```

### 4.2.9 modify data according to data

```//Modify data according to data
void SeqlistUpdateByData(seqlist *st,DataType OldValue,DataType NewValue)
{
int i,flag = 0;
for(i = 0;i <= st->pos;i++)
{
if(st->data[i] == OldValue)
{
st->data[i] = NewValue;
flag = 1;
}
}
if(flag == 0)
{
printf("Failed to modify data%d non-existent!",OldValue);
}
}```

### 4.2.10 modify data according to location

```//Modify data by location
void SeqlistUpdateByPos(seqlist *st,int p,DataType value)
{
if (p < 0 || p > st->pos +1)
{
printf("Incorrect location, modification failed!\n");
return;
}
else
{
st->data[p] = value;
printf("Modification succeeded!\n");
}
}```

### 4.2.11 find location according to data

```//Find location by data
int SeqlistSearchByData(seqlist *st,DataType value)
{
int i;
for ( i = 0; i <= st->pos; i++)
{
if(st->data[i] == value)
{
printf("Search succeeded!\n");
return i;
}
}
printf("Failed to find data%d non-existent",value);
return -1;
}
```

### 4.2.12 find data by location

```//Find data by location
DataType SeqlistSearchByPos(seqlist *st,int p)
{
if (p < 0 || p > st->pos +1)
{
printf("Wrong location, search failed!\n");
return (DataType) -1;
}
else
{
printf("Search succeeded!\n");
return st->data[p];
}
}```

## practice:

### 1. Delete duplicate data

(compare the first data with the following data, and delete the following data if there are duplicates)

s1: 1 2 2 2 1 1 3 4 2 4 5 4 1 -----> s1:1 2 3 4 5

```//Delete duplicate data
void SeqlistDeleteRepeat(seqlist *st)
{
int i,j;
for(i = 0;i < st->pos;i++)
{
for(j = i + 1;j <= st->pos;j++)
{
if(st->data[i] == st->data[j])
{
SeqlistDeleteByPos(st,j);
j--;
}
}
}
}
```

### 2. Consolidated statement

(save the data different from s1 in s2 behind s1)

s1: 1 2 3 4 5

s2: 1 3 5 7 9

...

s1:1 2 3 4 5 7 9

```//Consolidated statement
void SeqlistMerge(seqlist *s1,seqlist *s2)
{
int i;
for(i = 0;i < s2->pos;i++)
{
if(SeqlistSearchByPos(s1,s2->data[i]) == -1)
{
SeqlistInsert(s1,s2->data[i]);
}
}
}```