mzbzhchh 发表于 2015-12-9 20:27:21

56课-链表 参考码源


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct str)
struct str * CJLB();//创建链表 返回链表头 地址
void DYLB(struct str *head);//打印链表

struct str //自定义数据类型
{
    long xuehao;
    float chengji;
    struct str *next;

};
void main()

{
   
   
   
    DYLB(CJLB());
    /*
    DYLB 参数1:链表的首地址
    CJLB 没有参数 返回链表首地址

    */


   



   
   


   
   

}
struct str *CJLB()//定义创建链表 函数
{
    struct str *head,*p1,*p2;
   
   
    p1=(struct str*)malloc(LEN);//申请内存
   
   
   
   
   
    head=p1;//首地址
   
   
   
   
    printf("请输入学号:");
    scanf("%d",&p1->xuehao);//头地址
    printf("请输入成绩:");
    scanf("%f",&p1->chengji);//头地址

    p2=(struct str*)malloc(LEN);//申请内存
    p1->next=p2;//指向申请的内存的地址
    while (1)
    {   struct str *v;
      printf("请输入学号:");
      scanf("%d",&p2->xuehao);
      
      if(p2->xuehao ==0)//判断是否符合条件结束循环
      {
            v->next=NULL;

      break;
      
      }
      printf("请输入成绩:");
      scanf("%f",&p2->chengji);
      p1=(struct str*)malloc(LEN);
      p2->next=p1;
      v=p2;//记录P2的地址 如果下一次输入学号 0则让p2指向地址为 NULL


      p2=p1;
      
      
      
      
    }
    return head;


}
void DYLB(struct str *head)//定义打印链表 函数
{
    printf("***************************************\n");
   
    do
    {/*
      if (head->xuehao==0)
      {   head=head->next;
      continue;
      
      }
      */
      
      
      
      
      
      printf("学号:%d\n",head->xuehao);
      printf("成绩:%f\n",head->chengji);
      printf("-----------------------------\n");
      head=head->next;
      
      
    } while (head);
   
    printf("***************************************\n");
   
   
   


}

mzbzhchh 发表于 2015-12-9 20:31:39

眼看代码千篇,不如模仿一篇,模仿代码一篇,不如调试一篇
页: [1]
查看完整版本: 56课-链表 参考码源