C语言链表(linked list)基础知识总结

一、链表

1.概念:链表是一种物理存储单元上非连续、非顺序的存储结构。

2.组成:链表由一个个结点连接而成,每一个结点分为俩个主要部分。第一个部分是数据部分,用于存储该结点的数据。第二个部分为指针部分,每个结点的指针指向下一个结点的地址来完成链表连接的逻辑功能。

3.特点:存储地址不连续:不同于数组,数组的数据元素在物理存储单元上是连续的。而链表的数据在物理存储单元上一般不连续而且也没有什么顺序可言。

4.缺点:链表的定义和逻辑比数组要略微复杂一些,指针使用不当容易造成段错误以及一些逻辑错误。在查找某一个数据元素时需要遍历链表。

5.优点:每一个结点的数据部分可以包含多种、多个的数据。可以查找、插入、删除链表中的某个结点完成场景需要。使用起来比较灵活,适用于更多的复杂的场景需要。

二、链表的使用

1.链表的建立

1)创建一个结构体作为链表的一个结点。

struct Student     //创建一个结构体作为结点:关键字struct 
{
	int num;    //结点的数据部分
	struct node *next;   //结点的指针部分
};  //注意有分号 忘记了问题也不大

2)将创建的结点连接起来。

int main(){
	struct Student *s1; //定义结构体指针
    struct Student *s2;
    struct Student *s3;
	s1 = (struct Student *)malloc(sizeof(struct Student));  //为指针开辟一块内存
    s2 = (struct Student *)malloc(sizeof(struct Student));
    s3 = (struct Student *)malloc(sizeof(struct Student));
    s1->num = 1; //数据部分初始化
    s2->num = 2;
    s3->num = 3;
    s1->pNext = s2; //将结点中的指针指向下一个结点
    s2->pNext = s3;
    s3->pNext = NULL; //最后一个结点不指向任何地址
}

至此一个简单的链表就建立完成,链表包含三个结点。

3)链表的初始化

int initLink(struct Student *new, int i) {
    printf("please input No.%d'num\n", i);
    scanf("%d", &new->num);
    if (new->num == 0) {  //当输入num为零时不再增加新结点
        return -1;
    }
    printf("please input No.%d'score\n", i);
    scanf("%d", &new->score);
    printf("please input No.%d'name\n", i);
    scanf("%s", new->name);
}

4)*动态输入建立链表(尾插法)

struct Student *link() {
    struct Student *s1;
    struct Student *tail;
    struct Student *new;
    int i = 0;
    s1 = (struct Student *)malloc(sizeof(struct Student));
    tail = s1;
    while (1) {
        new = (struct Student *)malloc(sizeof(struct Student));
        if (initLink(new, i + 1) == -1) {
            break;
        }
        tail->pNext = new; //不断从链表尾部添加新的结点
        tail = new;
        tail->pNext = NULL;
        i++;
    }
    return s1;
}

3.链表的遍历

void bianli(struct Student *s1) {  //遍历函数的封装遍历时需要头结点的指针
    struct Student *p = s1;
    while (p != NULL) {
        printf("%d\n", p->num); //输出节点中的数据
        p = p->pNext;
    }
}

4.链表结点的查询


int serchNode(struct Student *s1, int num) {
    int position = 0;
    struct Student *p = s1;
    while (p != NULL) {
        if (p->num == 2) { 		//查找num=2的结点所在位置
            printf("num=2 in position %d\n", position);
            return 0;
            break;
        }
        // else if(p == NULL){        
        //    printf("can not find this node\n");
        //  break;
        //}
        else {
            p = p->pNext;
            position++;
        }
    }
    return 100;
}

5.链表结点的插入

int insertNode(struct Student *s1, char insertposition[], struct Student newNode) {
    int position = 0;
    struct Student *p = s1;
    while (p != NULL) {
        if (strcmp(p->name,insertposition) == 0) {       //1.找到需要插入结点的位置
            printf("insert node success \n");
            newNode.pNext = p->pNext;  //2.新节点的指针指向插入结点的下一个结点
            p->pNext = &newNode; //3.插入结点的指针指向新的结点
            bianli(s1->pNext);  // 插入成功打印一次链表
            return 0;
            break;
        }
        // else if(p == NULL){
        //    printf("can not find this node\n");
        //  break;
        //}
        else {
            p = p->pNext;
            position++;
        }
    }
    return 100;
}

6.链表结点的删除

void delNode(int number,struct Student *s1){
    struct Student *p = s1;
    while (p != NULL) {p = p->pNext;
        if (p->pNext->num == number) {   //1.找到需要删除的结点的上一个结点
            struct Student *tmp = p->pNext;
            p->pNext = p->pNext->pNext;  //2.将上一个结点的指针指向下下个结点
            free(tmp);         //3.释放所删除的结点的内存
            printf("delete success! \n");
            break;
        }        
    }
    bianli(s1->pNext); //打印删除后的链表
}

三、链表综合运用小程序

1.运用链表实现学生的 学号、名字、分数数据库的建立

2.运行结果展示

  • 16
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值