#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
int num;
float score;
struct Student *next;
};
struct Student *creatline(void)
{
int n=0;
struct Student *head, *p1, *p2;
p1=(struct Student *)malloc(LEN);
p2=(struct Student *)malloc(LEN);
head=NULL;
scanf("%d, %f", &p1->num, &p1->score);
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(LEN);
scanf("%d, %f", &p1->num, &p1->score);
}
p2->next=NULL;
return (head);
}
void printfdata(struct Student *head)
{
struct Student *pt;
pt=head;
do
{
printf("%d, %3.1f\n", pt->num, pt->score);
pt=pt->next;
}while(pt!=NULL);
}
struct Student *insert(struct Student *head, struct Student *stud) //該部分函式是否有問題?無法將結點插入至表頭位置!!
{
struct Student *p0, *p1, *p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return (head);
}
int main()
{
struct Student *head, *stud;
int num;
head=creatline();
printfdata(head);
stud=(struct Student *)malloc(LEN);
printf("請輸入您要插入的結點資訊:num=");
scanf("%d", &stud->num);
printf("score=");
scanf("%f", &(*stud).score);
insert(head, stud);
printfdata(head);
return 0;
}
//關于插入結點部分的函式是否有問題?測驗發現法將結點插入至表頭位置,但插入到其它位置均可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/146202.html
標籤:C語言
