• 创建动态单链表


    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    struct Student
    {
     long num;
     float score;
     struct Student* next;
    };
    int n;
    struct Student* creat(void)//构造一个函数,函数返回值为指针,该指针是指向结构体类型
    {
     struct Student* head;
     struct Student *p1,*p2;// 定义三个结构体指针
     n = 0;
     p1=p2 = (struct Student*)malloc(sizeof(struct Student));//给p1和p2创建一个结构体容量的内存空间
     scanf("%d,%f", &(*p1).num, &(*p1).score);//输入p1所指结构体内各元素值
     head = NULL;//赋值HEAD为空指针
     while (p1->num != 0)//判断p1指向的结点内number元素是否为0
     {
      n = n + 1;//n的值加1
      if (n == 1) head = p1;//head指向第一个结点
      else p2->next = p1;//如果n的值不为1,则使p2所指的结构体内next指针指向p1.
      p2 = p1;//把p1赋值给p2,即使p2指针向前移动到p1.
      p1 = (struct Student*)malloc(sizeof (struct Student));//继续给p1开辟空间
      scanf("%d,%f", &p1->num, &p1->score);//输入p1指向的新结点值。
      //scanf("%d,%f ", &p1->num, &p1->score);//此处需要特别注意多一个 需要多一行。在这卡了很久。
     }
     p2->next = NULL;//当p1所指结点中number元素为0时,赋值p2->next为空。
     return(head);//返回头指针
    }

    /*struct Student*creat(void)
    {
     struct Student *head;
     struct Student *p1, *p2;
     n = 0;
     p1 = p2 = (struct Student*)malloc(sizeof (struct Student));
     scanf("%d,%f", &(*p1).num, &(*p1).score);
     head = NULL;
     while (p1->num != 0)
     {
      n = n + 1;
      if (n == 1) head = p1;
      else p2->next = p1;
      p2 = p1;
      p1 = (struct Student*)malloc(sizeof (struct Student));
      scanf("%d,%f", &p1->num, &p1->score);
     }
     p2->next = NULL;
     return(head);
    }*/
    /*void printing(struct Student *head)
    {
    struct Student *p;
    p = head;
    if (head != NULL)
    do
    {
    printf("%1d%5.1f ", p->number, p->mark);
    p=p->next;
    } while (p != NULL);
    }*/
    int main()
    {
     struct Student* Creat(void);
     //void printing(struct Student* head);
     struct Student *pt;
     pt = creat();
     printf(" number:%1d mark:%5.1f ", pt->num, pt->score);
     //printing(head);
     return 0;
    }
     
     
     
  • 相关阅读:
    day07_final
    day06_final
    day02_final
    day04_final
    New
    AtCoder Grand Contest 015 E Mr.Aoki Incubator
    长链剖分学习笔记
    关于某些莫队的优化
    CodePlus 2019 3月月赛 Div.1 A题 TREE
    边分治学习笔记
  • 原文地址:https://www.cnblogs.com/pquan/p/10992325.html
Copyright © 2020-2023  润新知