#include<stdio.h>
#include<stdlib.h>
//鏈表的輸出
struct node
{
int num,score;
struct node *link;
};//定義結點型別
struct node*creat(int);
void print(node*);
void main()
{
node* head=0;
head=creat(2);
print(head);
getchar();
getchar();
}
struct node *creat(int n)
{
node *h=0,*p,*q;
int i;
for(i=1;i<=n;i++){
q=(node*)malloc(sizeof(node));
scanf("%d%d",&q->num,&q->score);
q->link=0;
if(h==0)h=q;
else
p->link=0;
p=q;//p指標指向表尾結點
}
return h;
}
void print(node*h)
{
node *p=0;
p=h;
do
{
printf("num=%d\tscore=%d\n",p->num,p->score);
p=p->link;
}
while(h);
}
uj5u.com熱心網友回復:
create和print函式里有BUG, 修正一下:struct node *creat(int n)
{
node *h = 0, *p;
int i;
for (i = 1; i <= n; i++)
{
p = (node *)malloc(sizeof(node));
scanf("%d%d", &p->num, &p->score);
p->link = 0;
if (h == 0)
h = p;
else
h->link = p;
}
return h;
}
void print(node*h)
{
node *p = h;
do
{
printf("num=%d\tscore=%d\n", p->num, p->score);
p = p->link;
} while (p);
}
uj5u.com熱心網友回復:
非常感謝,能再問一下我的錯誤是什么原因嗎uj5u.com熱心網友回復:
creat函式里,可能是你對鏈表的概念理解不到位,邏輯錯誤。print函式里,你原先的代碼,在回圈中,h的值不會發生變化,就成了死回圈了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/64516.html
標籤:基礎類
上一篇:這幾個程式怎么撰寫,求解大神。
下一篇:求解答,剛接觸c 語言
