課程
/后端開發(fā)
/C
/Linux C語言結(jié)構(gòu)體
怎樣寫輸出才能把所有的鏈表輸出出來
2018-04-18
源自:Linux C語言結(jié)構(gòu)體 5-2
正在回答
int?main(){ ??struct?weapon?*l,*k; ??*l=create(); ??while(l->next!=NULL){ ????printf("%d,%d\n",l->price,l->atk); ????k=l->next; ????l=k; ????} ??printf("%d,%d\n",l->price,l->atk); ??return?0;
}
/*忘了我的和老師的內(nèi)容不一樣了emmmm我寫的是stu和creat 想寫create的但是拼錯了咳咳*/#include <stdio.h>#include <malloc.h>struct stu{?int age;?char name[10];?struct stu *next;};struct stu * creat(){?struct stu *head;?struct stu *p1,*p2;?int n=0;?p1=p2=(struct stu *)malloc(sizeof(struct stu));?scanf("%d %s",&p1->age,&p1->name);?head=NULL;?while(p1->age!=0)?{?n++;?if(n==1)?head=p1;?else?p2->next=p1;
?p2=p1;?p1=(struct stu *)malloc(sizeof(struct stu));?scanf("%d %s",&p1->age,&p1->name);?}?free(p1);?p2->next=NULL;?return (head);}
int main(){?struct stu *p;?int i=0;?p=creat();?while(p->next!=NULL)?{??i++;??printf("%d %d %s\n",i,p->age,p->name);??p=p->next;?}?i++;?printf("%d %d %s\n",i,p->age,p->name);?return 0;}
做一個循環(huán)就可以了
舉報(bào)
C語言的深入,幫助小伙伴們進(jìn)一步的理解C語言,趕緊看過來
2 回答如果輸出全部的話怎么遍歷鏈表呢?
1 回答就輸出第一個節(jié)點(diǎn)的信息,如果把輸入的都輸出怎么辦?
1 回答atk 數(shù)值輸出的 問題
2 回答輸入第一遍0,0不跳出,第二遍才可以?;蛘咧惠斎?也可以一次性跳出
2 回答動態(tài)鏈表中,循環(huán)輸入scanf(“%d %d\n”,p->high,p->weight);中為什么添加了\n要輸入兩次0才會結(jié)束循環(huán)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2018-05-25
}
2019-07-10
/*忘了我的和老師的內(nèi)容不一樣了emmmm
我寫的是stu和creat 想寫create的但是拼錯了咳咳*/
#include <stdio.h>
#include <malloc.h>
struct stu{
?int age;
?char name[10];
?struct stu *next;
};
struct stu * creat()
{
?struct stu *head;
?struct stu *p1,*p2;
?int n=0;
?p1=p2=(struct stu *)malloc(sizeof(struct stu));
?scanf("%d %s",&p1->age,&p1->name);
?head=NULL;
?while(p1->age!=0)
?{
?n++;
?if(n==1)
?head=p1;
?else
?p2->next=p1;
?p2=p1;
?p1=(struct stu *)malloc(sizeof(struct stu));
?scanf("%d %s",&p1->age,&p1->name);
?}
?free(p1);
?p2->next=NULL;
?return (head);
}
2019-07-10
int main()
{
?struct stu *p;
?int i=0;
?p=creat();
?while(p->next!=NULL)
?{
??i++;
??printf("%d %d %s\n",i,p->age,p->name);
??p=p->next;
?}
?i++;
?printf("%d %d %s\n",i,p->age,p->name);
?return 0;
}
2018-04-21
做一個循環(huán)就可以了