A simple diagram of a linked list

The essence of a linked list is a pointer. Store the address of the next element.

typedef struct spy
{
    
    
	char * name ;
	struct spy * next;
}spy ,*p_spy;

spy A ={
    
    'A',NULL};
spy B ={
    
    'B',NULL};
spy C ={
    
    'C',NULL};

int main()
{
    
    
		p_spy =&A;
		A.next=&B;//A是一个结构体变量,A.next是对这个结构体变量的引用。next本身是一个结构体变量的指针,所以next只能赋值结构体变量的地址。即将B的地址赋值给next。
		B.next=&C;
		C.next=NULL;
		while(p_spy )
		{
    
    
			printf("%s\r\n",p_spy->name);
			p_spy=p_spy->next;
		}

};

When executed for the first time, p_spy saves the address of the structure variable A, so p_spy->name prints A for the first time; after executing p_spy
=p_spy->next;, the structure pointer p_spy saves the address of the next structure variable. p_spy is still not equal to 0, B is printed for the second time...C is printed for the third time.
Memory distribution map
Insert image description here

Guess you like

Origin blog.csdn.net/qq_35318223/article/details/131760486