#include<stdio.h>
#include<stdlib.h>
struct student{
long int num;//學號
float score;//成績
struct student*next;//指向下一個學生
};
int n=0;//有n個學生數據
/*創建鏈表函數*/
struct student* creat()
{
struct student *p1,*p2,*head;
p2=p1=(struct student*)malloc(sizeof(struct student));//創建一個!!動態存儲空間
printf("please enter the first student:\n");
scanf("%ld,%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("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
/*輸出函數*/
void print(struct student *head)
{
struct student *p1;
p1=head;
while(p1->num!=0)
{
printf("\nnum:%ld,score:%f",p1->num,p1->score);
p1=p1->next;
}
}
void main()
{
struct student *head;
head=creat();
printf("plese printf the num and score\n");
print(head);
}
數據結構要把我折磨瘋了,視頻我已經看了四五遍了,自己寫代碼還是有問題。。很崩潰。慢慢來。!
結構體創建和結構體指針,得到了提高。函數調用,該調用哪個參數,返回值應該是哪個變量,很重要!
關於scanf,鍵盤輸入時,要與scanf里面格式要一模一樣,空格都空格,用逗號時記得運行界面輸入法轉為英文!!!!