Re: [問題] C語言的link list問題

看板C_and_CPP作者 (艾斯寇德)時間14年前 (2009/12/13 18:31), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串4/4 (看更多)
※ 引述《jacky1989 (幻想的夢境)》之銘言: 我最近在寫link list 不過遇到一點小問題 #include<alloc.h> struct list{ float coef; int exp; struct list *nx; }; struct list *p1,*fr,*nw,*dl; int main() { int x,a[]={5,0,4,1,3,2},b[]={6,0,9,2},i; float y; p1=(struct list *)malloc(sizeof(struct list)); nw=(struct list *)malloc(sizeof(struct list)); 這裡整個只有p1,nw兩個物件?其他插入的都不是list ? fr=p1; for(i=1;i<6;i+=2){ fr->exp=a[i]; nw->nx=fr; fr=nw; nw=0; } while(fr!=NULL){ printf("p1x=%d\n",fr->exp); dl=fr; fr=fr->nx; free(dl); } } 你應該把linked list的new操作寫完整一點。 把他抽出來的話是 struct list* new_node(float coef,int exp){ struct list* ret = (struct list*)malloc(sizeof(struct list)); ret->coef = coef; ret->exp = exp; ret->nx = 0; return ret; } 並且使整體有一個back (我不這麼幹的,另外我討厭全域變數, 我習慣用一個外覆struct包裝整個list的size,front,back) back->nx = new_node(a[ i ],b[ i ]); back = back->nx; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.171.90.29

12/13 18:36, , 1F
我們上課沒有說到這麼多耶...
12/13 18:36, 1F
文章代碼(AID): #1B9CAPYF (C_and_CPP)
文章代碼(AID): #1B9CAPYF (C_and_CPP)