程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c語言-C語言鏈表創建問題..123

c語言-C語言鏈表創建問題..123

編輯:編程綜合問答
C語言鏈表創建問題..123

#include"stdafx.h"
#include
#define LEN sizeof(student)
struct student{
int num;
float score;
student *next;
};
student * creat(){
int num;
float score;
student *head, *p,*q;

q=p=head = (student *)malloc(LEN);
head->num = NULL;
scanf_s("%d%f",&num,&score);
while (num != 0)//如果輸入0的話就當成結束
{
    p = (student*)malloc(LEN);
    p->num = num;
    p->score = score;
    p->next = NULL;
    q->next = p;
    q = p;
    scanf_s("%d%f",&num,&score);
}
p->next = NULL;
return head;

}
void print(student * head){
student *p;
p = head;
for (; p->next != NULL;)
{
printf("%d %f", p->next->num, p->next->score);
p = p->next;
}

}
void main(){
student *head;
creat();
head = creat();
print(head);

}
//我輸入
1 20
2 30
3 40
0 10
的話沒有輸出什麼東西啊

最佳回答:


主函數裡為什麼會有兩個creat函數調用,我把你的代碼測試了一下,去掉第一個creat 是可以運行成功的,也有輸出信息。另外你的代碼缺少標准庫能編譯通過嗎?希望有所幫助。
#include
#include
#define LEN sizeof(student)

struct student{
int num;
float score;
student *next;
};

student * creat(){
int num;
float score;
student head, *p,*q;
q=p=head = (student *)malloc(LEN);
head->num = NULL;
scanf("%d%f",&num,&score);
while (num != 0)//如果輸入0的話就當成結束
{
p = (student
)malloc(LEN);
p->num = num;
p->score = score;
p->next = NULL;
q->next = p;
q = q->next;
scanf("%d%f",&num,&score);
}
p->next = NULL;
return head;

}
void print(student * head){
student *p;
p = head;
for (; p->next != NULL;)
{
printf("%d %f", p->next->num, p->next->score);
p = p->next;
}

}
int main(){
student *head;
head = creat();
print(head);

return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved