#include "stdio.h"#include<malloc.h>#include<string.h>#include<ctype.h>#define LEN struct studentstruct student{ char name[22]; int Chinese; int Math; int English; struct student*next;};struct student*create(){ char ch;? ? struct student *head;? ? struct student*p1,*p2;? ? int a=0;? ? printf("是否輸入新數(shù)據(jù):\n");? ?? ? ?while(toupper(ch=getchar())=='Y')? ? {? ? ? p1=(LEN*)malloc(sizeof(LEN));? ? ? printf("name:");? ? ? scanf("%s",&(*p1).name);? ? ? printf("Chinese:");? ? ? scanf("%d",&(*p1).Chinese);? ? ? printf("Math:");? ? ? scanf("%d",&(*p1).Math);? ? ? printf("English:");? ? ? scanf("%d",&(*p1).English);? ? ? getchar();//getchar吸收回車? ? ? printf("是否輸入新數(shù)據(jù):\n");? ? ? a++;? ? ? if(a==1)? ? ? ?head=p1;? ? ? else ? ? ? ?(*p2).next=p1;? ? ? p2=p1;? ? ? p1->next=NULL;? ? ? }? ? return(head);}struct student * insert(struct student * head){? ? struct student *p0,*p1,*p2;? ? p0=(struct student * )malloc(sizeof(LEN));? ? printf("\n輸入所要插入節(jié)點的數(shù)據(jù):");? ? scanf("%ld,%f",&num,&score);? ? p1=head;? ? if(head==NULL)? ? {? ? ? head=p0;? ? ? p0->next=NULL;? ? }? ? else? ? if(p1->num>p0->num)? ? {? ? //p0->next=head;? ? head=p0;//可以刪掉上一行嗎,p0賦給head,? ? //p0->next不會自動指向下一個節(jié)點嗎.因為p0包含了next? ? ? ?//不是嗎? ? }? ? else? ? {? ? while(p1!=NULL)? ? {? ? if(p1->num<p0->num)? ? {? ? p2=p1;? ? p1=p1->next;? ? }? ? else? ? {? ? p0->next=p1;? ? p2->next=p0;? ? break;? ? }? ? }? ? if(p1->next=NULL;? ? {? ? p0->next=NULL;? ? p2->next=p0;? ? }? ? ? ? }? ? return(head); ??}void print(struct student *head){ struct student *p; p=head; while(p!=NULL) { printf("\nname:%-8sChinese:%-5dMath:%-5 dEnglish:%-5d",p->name,p->Chinese,p->Math,p->English);//只是一個名 p=p->next;//測試隨便輸 }}void main( ){ //struct student *stu; //stu=create(); print(create());}
關(guān)于結(jié)構(gòu)體指針的問題,只看我發(fā)的圖片就可以了
溯源1
2017-02-02 11:16:58