c语言——单向链表创建(头插法和尾插法)

#include <stdio.h>
#include <stdlib.h>
int n;
typedef struct Student
{
    int data;
    struct Student *next;
}Stu;
Stu *creat()
{
    Stu *head,*p,*q;
    head   = (Stu*)calloc(1,sizeof(Stu));
    if(!head) exit(0);
    head->next = NULL;
    scanf("%d",&n);
    //头插法
    /*for(int i=0;i<n;i++) { p = (Stu *)calloc(1,sizeof(Stu)); if(!p) exit(0); scanf("%d",&p->data); p->next = head->next; head->next=p; }*/
    //尾插法
    q = head;
    for(int i=0;i<n;i++)
    {
        p = (Stu *)calloc(1,sizeof(Stu));
        if(!p) exit(0);
        scanf("%d",&p->data);
        q->next = p;
        q = p;
    }
    q->next = NULL;//此时q就是尾结点*/
    return head;
}
int main()
{
    Stu *head,*p;
    head = creat();
    for(p=head->next;p;p=p->next)
    {
        printf("%d ",p->data);
    }
    return 0;
}

全部评论

相关推荐

11-13 10:17
门头沟学院 Java
昨天面美团,jvm,juc问的好深啊,感觉小林coding不太够喔,牛油们有没有什么推荐的八股网站嘛🕒&nbsp;岗位/面试时间👥&nbsp;面试题目🤔&nbsp;面试感受
明天不下雨了:小林Coding:https://xiaolincoding.com/ 全栈哥:https://www.pdai.tech/ Guide哥:https://javaguide.cn/ 秀哥:https://interviewguide.cn/ 沉默王二:https://javabetter.cn/home.html 磊哥:https://www.javacn.site/interview/basic/ 小傅哥:https://bugstack.cn/ 源码哥:https://doocs.github.io/source-code-hunter/#/ 各大厂的公众号技术文章和一些经典的书籍
面试太紧张了怎么办?
点赞 评论 收藏
分享
11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务