购物网站黑白,电商平台首页设计,微信公众号和微网站,品牌网店看如下代码#xff0c;这是一个完整的可运行的c源文件#xff0c;要注意的点#xff1a;
c语言程序运行不一定需要头文件NULL其实是 (void*)0#xff0c;把指针赋值成(void*)0,就是防止程序员不想该指针被引用的时候被引用#xff0c;引用地址为0的值程序会引起系统中断这是一个完整的可运行的c源文件要注意的点
c语言程序运行不一定需要头文件NULL其实是 (void*)0把指针赋值成(void*)0,就是防止程序员不想该指针被引用的时候被引用引用地址为0的值程序会引起系统中断程序会异常终止可能会有人觉得别人讲链表都是malloc怎么这里没有mallocmalloc是用来申请内存的比较灵活可以自由改变链表长度这里用赋值的方法是因为从最简单的开始讲更能讲清楚原理
#define NULL (void*)0
struct list{int num;/*单向链表需要一个结构体指针指向下一个节点的地址如果是尾巴节点则是NULL*/struct list *next;
};
int main()
{struct list n0 {100, NULL};struct list n1 {29, NULL};struct list n2 {78, NULL};struct list n3 {24, NULL};n0.next n1;n1.next n2; // 等价 n0.next-next n2n2.next n3; // 等价 n0.next-next-next n3return 0;
}我们先执行完main函数里面前四句赋值语句
用gdb查看n0, n1, n2, n3的地址如下他们的地址都是不一样的因为他们在内存的不同的地方他们也没有什么关系你们运行出来的内存地址应该和我的不一样无需产生疑惑
他们在你的内存条里像这样分布没什么特点就是四个结构体他们占用的大小一样都是sizeof(struct list)
我们再查看他们的值都是初始化的值没错
执行 n0.next n1;
就是把n1的地址挂在n0的尾巴上next指针这样就可以通过n0.next来表示n1查看n0.next发现变成了n1的地址
执行 n1.next n2;和 n2.next n3
经过上面的解释这两句应该很好理解就是把n2的地址挂在n1的next上n3的地址挂在n2的next上现在就组成了一个简单的链表就可以通过结构体n0通常称之为头节点访问这个链表上的任何数据
使用malloc申请内存的方式创建链表
#include stdlib.h
struct list{int num;struct list *next;
};
int main()
{struct list *tmp_node;struct list *head (struct list*)malloc(sizeof(struct list));head-num 10;head-next NULL;/*添加节点*/tmp_node (struct list*)malloc(sizeof(struct list));tmp_node-num 3;tmp_node-next NULL;head-next tmp_node;/*添加节点*/tmp_node (struct list*)malloc(sizeof(struct list));tmp_node-num 5;tmp_node-next NULL;head-next-next tmp_node;/*添加节点*/tmp_node (struct list*)malloc(sizeof(struct list));tmp_node-num 80;tmp_node-next NULL;head-next-next-next tmp_node;return 0;
}这段代码并不难看懂就是创建一个节点然后挂在head的尾巴上可是这里有缺陷就是每次都要自己记住有几个节点然后用 head-next-next-next tmp_node;的语句去接上节点这样太麻烦了我们可以再创建一个尾节点来表示这个链表的尾巴节点。
#include stdlib.h
struct list{int num;struct list *next;
};
int main()
{struct list *tmp_node;struct list *head (struct list*)malloc(sizeof(struct list));head-num 10;head-next NULL;/*刚开始头就是尾这个能理解吧*/struct list *tail head;/*添加节点*/tmp_node (struct list*)malloc(sizeof(struct list));tmp_node-num 3;tmp_node-next NULL;tail-next tmp_node; //接到尾巴上tail tail-next; //因为后面有新的节点尾巴要更新位置了移到新节点了/*添加节点*/tmp_node (struct list*)malloc(sizeof(struct list));tmp_node-num 5;tmp_node-next NULL;tail-next tmp_node;tail tail-next;/*添加节点*/tmp_node (struct list*)malloc(sizeof(struct list));tmp_node-num 80;tmp_node-next NULL;tail-next tmp_node;tail tail-next;return 0;
}记住两点
头节点一般不移动每一次操作完毕后尾节点都是指向最后一个节点的地址
取下/删除链表节点
增加链表节点
插入链表节点
待更新