美文网首页
图解算法之单向链表的建立

图解算法之单向链表的建立

作者: Pepi熊 | 来源:发表于2020-09-11 11:03 被阅读0次
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
    char name[20];
    int score;
    student *next;
};
//【1】循环建立n个单向链表函数,返回头节点
student* CreateList(student *head, int n)//n是循环次数
{
    //找到最后一个节点
    student* p = head;
    while (p->next != NULL)
    {
        p = p->next;
    }
    //
    while(n--)
    {
        student* p1 = (student*)malloc(sizeof(student));
        p->next = p1;
        scanf("%s %d",&p->name,&p->score);//数据域用户自己输入
        p1->next = NULL;//指针域指向无
        p = p1;//更新p的位置
    }
    return head;
}

//打印链表函数
void Print(student* head)
{
    student* p = head;
    int i = 0;
    while (p->next != NULL)
    {
        i++;
        printf("第%d个同学: 姓名:%s\t分数:%d\n",i, p->name, p->score);
        p = p->next;
    }
}
int main()
{
    //【1】
    int n = 4;
    student *head = (student*)malloc(sizeof(student));//head不要放东西
    //strcpy(head->name,"小明");//结构体不能直接对字符串数组赋值!!!

    head->next = NULL;//head只放指针域
    head = CreateList(head, n);
    Print(head);

    //【2】

    return 0;
}

相关文章

  • 图解算法之单向链表的建立

  • 8.单向链表SingleLinkList

    目录:1.单向链表的定义2.单向链表的图解3.单向链表定义操作4.单向链表的实现 1.单向链表的定义 2.单向链表...

  • 10.单向循环链表SingleCycleLinkList

    目录:1.单向循环链表的定义2.单向循环链表的图解3.单向循环链表定义操作4.单向循环链表的实现 1.单向循环链表...

  • 数据结构 - 单向链表及相关算法

    单向链表 链表常见算法 链表反转

  • 单向链表的基本操作

    参考 一步一步写算法(之单向链表) 1. 单向链表的数据结构 2. 创建链表 3. 增加节点 4. 删除节点

  • 图解算法:单向链表做加法运算

    问:给出两个非空的链表,来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且每个结点只能存储一位...

  • 总结

    Android篇 数据结构与算法顺序表 - ArrayList源码链表 - 单向链表、双向链表 - LinkedL...

  • 单向链表算法

    单向链表 反转单向链表 单链表查找倒数第k个节点 单链表递归倒序打印 单链表排序 单链表删除重复节点

  • python链表及算法

    实现了python单向链表及一些算法题

  • 倒置链表

    题目描述:给出单向链表的头节点l,如何只遍历一次就将链表中的所有元素倒转。 算法描述: 首先给出单向链表的结构 遍...

网友评论

      本文标题:图解算法之单向链表的建立

      本文链接:https://www.haomeiwen.com/subject/ywmvektx.html