小蚂蚁 发表于 2021-12-5 22:05:23

[C语言] 链表的实现

静态链表#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

//静态链表
struct LinkNode
{
int num; //数据域
struct LinkNode* next;//指针域
};

int main()
{
//创建节点
struct LinkNode node1 = { 10,NULL };
struct LinkNode node2 = { 20,NULL };
struct LinkNode node3 = { 30,NULL };
struct LinkNode node4 = { 40,NULL };
struct LinkNode node5 = { 50,NULL };

//建立关系
node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = &node5;

//遍历链表
struct LinkNode* pCurrent = &node1;
while (pCurrent != NULL)
{
    printf("%d\n",pCurrent->num);
    pCurrent = pCurrent->next;
}
return 0;
}动态链表
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
//动态链表
struct LinkNode
{
int num; //数据域
struct LinkNode* next;//指针域
};

int main()
{
//创建节点 开辟空间
struct LinkNode *node1 = malloc(sizeof(struct LinkNode));
struct LinkNode *node2 = malloc(sizeof(struct LinkNode));
struct LinkNode *node3 = malloc(sizeof(struct LinkNode));
struct LinkNode *node4 = malloc(sizeof(struct LinkNode));
struct LinkNode *node5 = malloc(sizeof(struct LinkNode));

//给数据域赋值
node1->num = 10;
node2->num = 20;
node3->num = 30;
node4->num = 40;
node5->num = 50;

//建立关系
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
node5->next = NULL;

//遍历链表
struct LinkNode * pCurrent = node1;
while (pCurrent != NULL)
{
    printf("%d\n", pCurrent->num);
    pCurrent = pCurrent->next;
}

//释放
free(node1);
free(node2);
free(node3);
free(node4);
free(node5);

node1 = NULL;
node2 = NULL;
node3 = NULL;
node4 = NULL;
node5 = NULL;
return 0;
}自我总结:

[*] node.num      node->num   两者的区别是node类型不同
[*]动态链表最后一个节点的指针域要置空 防止野指针

https://blog.51cto.com/u_15335178/4751827
页: [1]
查看完整版本: [C语言] 链表的实现