【无标题】

     1) 结构体变量的首地址能够被其最宽基本类型成员的大小所整除;

     2) 结构体每个成员相对结构体首地址的偏移量(offset)都是成员大小的整数倍,如有需要编译器会在成员之间加上填充字节(internal adding);

     3) 结构体的总大小为结构体最宽基本类型成员大小的整数倍,如有需要编译器会在最末一个成员之后加上填充字节{trailing padding}。

【无标题】_第1张图片

【无标题】_第2张图片

 【无标题】_第3张图片

 1

 内核链表:

链表:增删改查

单向链表 双向链表。。。

线性数据结构:

数组、链表、堆、栈

数组:连续(物理空间),随机存取,插入删除慢,复杂,会整体偏移

链表:连续(逻辑),无法随机存取,插入和删除方便

堆:malloc()  free()

栈:先进后出

队列:先进先出

非线性:

树、图

树:文件系统(开发)

图:路径规划(百度地铁、高德、路由器(路由算法))

【无标题】_第4张图片

 【无标题】_第5张图片

 插入的代码是重点!!!!【无标题】_第6张图片

 【无标题】_第7张图片
【无标题】_第8张图片

 

#include 
#include 
#include "list.h"

struct student
{
    int age;
    char name[64];
    struct list_head list;
};

void main()
{
    struct list_head head;
    INIT_LIST_HEAD(&head);

    struct student stu1;
    strcpy(stu1.name, "zhangsan");
    stu1.age = 1;

    struct student stu2;
    strcpy(stu2.name, "lisi");
    stu2.age = 2;

    struct student stu3;
    strcpy(stu3.name, "wangwu");
    stu3.age = 3;


    list_add(&stu1.list, &head);  //插入
    list_add(&stu2.list, &head);
    list_add(&stu3.list, &head);

    struct list_head *pos;
    struct student *tmp;

    printf("init list\n");
    list_for_each(pos, &head)   //遍历
    {
        tmp = list_entry(pos, struct student, list);  
        //list 为 struct list_head 的名字  struct list_head list
        printf("name = %s, age = %d\n", tmp->name, tmp->age);
    }
    printf("\n");

    pos = get_first(&head);
    tmp = list_entry(pos, struct student, list);
    printf("first is %s\n\n", tmp->name);

    pos = get_last(&head);
    tmp = list_entry(pos, struct student, list);
    printf("last is %s\n\n", tmp->name);

    puts("del last");
    list_del(pos);

    printf("after del:");
    list_for_each(pos, &head)
    {
        tmp = list_entry(pos, struct student, list);
        printf("%d ", tmp->age);
    }
	puts("\n");
}

 

你可能感兴趣的:(链表,数据结构)