zoukankan      html  css  js  c++  java
  • 创建动态单链表

    #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;
    }
     
     
     
  • 相关阅读:
    hp一體機cartridge error及carriage jam4/22
    指纹仪zkonline.ocx:access violation...4/13
    IIS6:Service Unaviable 9/27
    寶寶的成長腳印3/15
    vs2003不能调试4/8
    C++ 的复制构造函数
    导入与导出数据 大容量复制程序(bcp)
    关于SQlserver数据库的加密应用
    DataGridView使用技巧
    使用C# 向记事本窗口发送消息
  • 原文地址:https://www.cnblogs.com/pquan/p/10992325.html
Copyright © 2011-2022 走看看