Demo entry 6678170

sds

   

Submitted by asds on Dec 05, 2017 at 12:26
Language: C. Code size: 1.7 kB.

#include <stdio.h>
#include <stdlib.h>
typedef struct student{
    int Num;
    char Name[15];
    double Score;
    struct student * next;
}student;
student *create_link(){
    student *head;
    student *p;
    head = (student*)malloc(sizeof(student));
    FILE * r= fopen("/Users/apple/Desktop/stud/Students.txt","r");
    if(r==NULL){
        printf("打开文件失败!");
        return NULL;
    }
    fscanf(r,"%d%s%lf",&head->Num,head->Name,&head->Score);
    head->next=NULL;
    p=head;
    p=(student*)malloc(sizeof(student));
    while(fscanf(r,"%d%s%lf",&p->Num,p->Name,&p->Score)&&p->Num!=0){
        p->next=NULL;
        if(p->Score>head->Score){
            p->next=head;
            head=p;
        }else if(p->Score<=head->Score){
            if(head->next==NULL)head->next=p;
            else{
            student *k;
            student *q;
                q=head;
                k=q;
            while(p->Score<=q->Score&&q->next!=NULL){
                k=q;
                q=q->next;
            }
                if(p->Score>q->Score){
                k->next=p;
                p->next=q;
                }
                else if(p->Score<=q->Score){
                    q->next=p;
                }
             }
           }
        p=(student*)malloc(sizeof(student));
      }
  return head;
}
void display(student *head){
    student *p;
    p=head;
    while(p!=NULL){
        printf("%d ",p->Num);
        printf("%s ",p->Name);
        printf("%.1lf",p->Score);
        printf("\n");
        p=p->next;
    }
}
int main(){
    student * head;
    head=create_link();
    display(head);
    return 0;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).