Lists are used for both database and data structures. So review the list of C (really forget it).
struct: (If a pointer to a struct type is defined in the struct, it is well suited for the establishment of a linked list)
eg
struct student{ int num; float score; struct Student * next;};
A static list requires that each node be initialized beforehand. That is, they must be represented by struct variables. That is to know in advance to enter a few sets of data.
Three groups are defined here.
1#include <stdio.h>2 structStudent3 {4 intnum;5 floatscore;6 structStudent *Next;7 };8 intMain ()9 {Ten structStudent a,b,c; One //a minimum of two pointers is required to meet the operational needs A structStudent *head,*p; -a.num=10101; a.score=89.5; -b.num=10103; b.score= -; thec.num=10107; c.score= -; -Head = &a;//Head Pointer -A.next = &b; -B.next = &C; +C.next =NULL; - //so far, all the nodes in the list have been assigned. +P=head;//this pointer is required by the Operation A Do at { -printf"%ld%5.1f\n",p->num,p->score); -p = p->Next; -} while(p!=NULL); - return 0; -}
The static list is quite simple. Here's a list of dynamic links.
Dynamic linked list is to open up a node from scratch. Need to review the dynamic allocation of memory knowledge points.
Global variables are assigned to "static storage", and local variables are assigned to "dynamic storage" (called a stack).
"Free Storage" "is also a dynamic store" (called a heap), used to hold temporary data, not to be declared as a variable, at any time to open up, do not have to release at any time. Accessible only through pointers.
The malloc function prototype void * malloc (unsigned int size) returns an untyped specific address. (Must use Stdlib.h header file)
Encyclopedia: sizeof is an operator (operator) in C/s + +, which simply means that it returns the number of bytes of memory that an object or type occupies.
The int* pointer only points to the INT type variable, so the address type also needs to be converted. such as the following code comment.
1#include <stdio.h>2#include <stdlib.h>3 #defineLEN sizeof (struct Student)4 structStudent5 {6 Longnum;7 floatscore;8 structStudent *Next;9 };Ten intn =0; One structStudent *creat () A { - structStudent *head; - structStudent *p1,*P2; the //(struct student *) is a forced type conversion to convert its first address to struct type (because malloc returns an untyped address) -P1 = (structStudent *) malloc (LEN); -scanf"%d%f",&p1->num,&p1->score); - while(p1->num!=0) + { -n++; + if(n==1) head =P1; A ElseP2->next =P1; atP2 =P1; -P1 = (structStudent *) malloc (LEN); -scanf"%d%f",&p1->num,&p1->score); - } -P2->next =NULL; - returnhead; in } - voidPrintstructStudent *head) to { + structStudent *p; -p =head; the while(p!=NULL) * { $printf"%d,%5.1f\n",p->num,p->score);Panax Notoginsengp = p->Next; - } the } + voidMain () A { the print (creat ()); +}
Static linked list and dynamic linked list