链表是一种应用的很广泛的数据结构,在内存中不连续的分布,下面将介绍链表的增、删、改、查。
链表的每一个节点都是一个结构体,val是这个节点的值,next是下一个节点的地址
typedef struct List{
int val;
struct List *next;
}List;
链表的插入,root是链表的头结点,value是要插入的值,默认插入到链表的尾端
List * listInsert(List *root,int value){
if(root == NULL){
root = (List*)malloc(sizeof(List));
root->val = value;
root->next = NULL;
return root;
}
struct List *l = root;
while(l->next != NULL){
l = l->next;
}
struct List* p = (List*)malloc(sizeof(List));
p->val = value;
p->next = NULL;
l->next = p;
return root;
}
当root是空的时候,位root分配内存,赋值,否则,找到当前链表的最后一个节点,赋值,返回
链表的插入,这个可以指定插入的位置(从0开始),i就是要插入的位置
List* listInsert(List *root,int value,int i){
struct List* p = root;
if(i == 0){