課程
/后端開發
/C
/Linux C語言結構體
怎樣寫輸出才能把所有的鏈表輸出出來
2018-04-18
源自:Linux C語言結構體 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;
}
/*忘了我的和老師的內容不一樣了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;}
做一個循環就可以了
舉報
C語言的深入,幫助小伙伴們進一步的理解C語言,趕緊看過來
2 回答如果輸出全部的話怎么遍歷鏈表呢?
1 回答就輸出第一個節點的信息,如果把輸入的都輸出怎么辦?
1 回答atk 數值輸出的 問題
2 回答輸入第一遍0,0不跳出,第二遍才可以。或者只輸入0也可以一次性跳出
2 回答動態鏈表中,循環輸入scanf(“%d %d\n”,p->high,p->weight);中為什么添加了\n要輸入兩次0才會結束循環
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-05-25
}
2019-07-10
/*忘了我的和老師的內容不一樣了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
做一個循環就可以了