Summary:
Linux Kernel uses a lot of Data Structure Knowledge. Although Linux is written in C language, many of the content in it is an object-oriented idea. Therefore, the knowledge of data structures is very basic and important.
Data structure refers to the logical structure, storage structure, and operations of data:
- Logical Structure of data
Linear Structure: 1. Linear Table 2. Stack 3. Queue
Nonlinear Structure: 1. Sequential storage 2. Graphic Structure
Sequential Storage
Chain Storage
- Data Operations: retrieval, sorting, insertion, deletion, modification, etc.
Write a linear table by yourself. Keep it for yourself later.
Sequential storage structure: (in fact, the return value of a function must be determined, so it is perfect. Functions in the Linux Kernel generally have return values. In general, an error is returned-1. If it is correct, it is only true.
The return value is 0, so I think the defined function should follow this rule. If the statement is judged to be followed by only one sentence, you are advised not to use {}. The Linux kernel will issue an alert .)
#include <stdio.h>#include <stdlib.h>#define N 10typedef int datatype;typedef struct { datatype data[N]; int last;}sqlist;sqlist * create_sqlist(){ sqlist *L; if((L = (sqlist *)malloc(sizeof(sqlist)))<0) { printf("malloc error!"); return NULL; } L->last = -1; return L;}int insert(sqlist * L,datatype data,int i){ int j; if((L->last >= N-1)||(i < 0)||(i > L->last+1)) return -1; for(j = L->last+1;j>i;j--) { L->data[j-1] = L->data[j]; } L->last++; L->data[i] = data; return 0;}int isempty(sqlist *L){ return (L->last == -1) ;}int delete(sqlist *L,int i){ int j; if((L->last == -1)||(i < 0)||(i > L->last)) return -1; for(j = i; j<= L->last; j++) { L->data[j] = L->data[j+1]; } L->last--; L->data[j+1] = 0; return 0;}int show(sqlist * L){ int i; if(L->last ==-1) { printf("empty!\n"); return -1; } for(i = 0;i <= L->last;i++) { printf("data[%d] = %d\n",i,L->data[i]); } return 0;}int main(int argc,char * argv[]){ int i; int ret; sqlist * L; L = create_sqlist(); for(i = 0;i<10;i++) { ret = insert(L,i,i); if(ret == -1) { printf("insert error\n"); return -1; } } ret = show(L); if(ret == -1) printf("show error\n"); printf("****************\n"); ret = delete(L,4); if(ret == -1) printf("delete error\n"); ret = show(L); if(ret == -1) printf("show error\n"); return 0;}
Chain storage structure:
# Include <stdio. h>
# Include <errno. h>
# Include <stdlib. h>
# Define N 10
Typedef int datatype;
Typedef struct node {
Datatype data;
Struct node * next;
} Linknode, * linklist;
Linklist create ()
{
Linklist h;
If (H = (linklist) malloc (sizeof (linknode) = NULL)
{
Perror ("malloc ");
Exit (-1 );
}
H-> next = NULL;
Printf ("create \ n ");
Return h;
}
Int lenth (linklist H)
{
Int I = 0;
While (h-> next )! = NULL)
{
H = H-> next;
I ++;
}
Return I;
}
Int insert (linklist H, datatype data, int POS)
{
Int I;
Linklist Q;
Linklist P = NULL;
P = h;
If (Pos <0) | (Pos> lenth (H )))
{
Printf ("insert error \ n ");
Return-1;
}
If (q = (linklist) malloc (sizeof (linknode) = NULL)
{
Perror ("malloc ");
Exit (-1 );
}
For (I = 0; I <Pos; I ++)
{
P = p-> next;
}
Q-> DATA = data;
Q-> next = p-> next;
P-> next = Q;
Return 0;
}
Int Delete (linklist H, int POS)
{
Int I;
Linklist P;
P = h;
If (Pos <0) | (Pos> lenth (H )))
{
Printf ("delete error \ n ");
Exit (-1 );
}
For (I = 0; I <Pos; I ++)
H = H-> next;
P = H-> next;
H-> next = p-> next;
Free (P );
Return 0;
}
Int relist (linklist H)
{
Linklist p, q;
P = H-> next;
H-> next = NULL;
While (P! = NULL)
{
Q = P;
P = p-> next;
Q-> next = H-> next;
H-> next = Q;
}
Return 0;
}
Int show (linklist H)
{
Linklist P;
P = h;
While (P-> next )! = NULL)
{
Printf ("Data = % d \ n", p-> data );
P = p-> next;
}
Return 0;
}
Int main (INT argc, char * argv [])
{
Int I = 0;
Int ret;
Linklist h;
H = create ();
For (I = 0; I <10; I ++)
{
Ret = insert (H, I, I );
If (ret =-1)
Printf ("insert error \ n ");
}
Printf ("*********** create linklist ************** \ n ");
Ret = show (h );
If (ret =-1)
Printf ("Show ERROR \ n ");
Printf ("************ delete 3 ********** \ n ");
Ret = Delete (h, 3 );
If (ret =-1)
Printf ("delete error \ n ");
Ret = show (h );
Printf ("*********** reservelist ********* \ n ");
Ret = relist (h );
If (ret =-1)
Printf ("reservelist error \ n ");
Show (h );
Return 0;
}
Differences between sequential storage and chained storage:
1. The storage location of logically adjacent elements is also adjacent;
2. Sequential storage Random Access to data elements or address-based access;
3. High Storage Density
4. the time complexity of inserting and deleting elements in a table is poor.
5. Insert and other operations are time-consuming, and elements must be moved in slices.