[問題] Linkedlist用法

看板C_and_CPP作者 (幻想的夢境)時間12年前 (2011/12/20 17:05), 編輯推噓0(006)
留言6則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 2008 問題(Question): 我打算用副程式讀入已存在的資料 用Linkedlist來建立 我希望可以由副程式讀完後將此串結傳回主程式使用 怎麼寫呢?? 我嘗試使用return 可是編譯器說是錯的 老師也沒有教怎麼傳遞 還是linkedlist無法傳遞?? 這是程式碼 #include"linklist.h" #include<stdio.h> #include<stdlib.h> #define np 3 int initial(){ FILE *fi; char tmp[8]; int i; fi=fopen("address.txt","r"); if(fi==0){ printf("File does not exist.\n"); system("pause"); return -1; } for(i=0;i<np;i++){ current=(NODE *)malloc(sizeof(NODE));//create new node fscanf(fi,"%s%s%s%s",&tmp,&current->name,&current->nickname,&current->cellphone); if(i==0) first=current; else previous->next=current; current->next=NULL; previous=current; } current=first; while(current!=NULL){ printf("Name:%s\n",current->name); printf("Nickname:%s\n",current->nickname); printf("Cellphone:%s\n",current->cellphone); printf("\n"); current=current->next; }; return first; } 這是我自定義的header file struct node{ char name[8]; char nickname[255]; char cellphone[10]; char telphone[10]; char birthday[10]; char address[255]; struct node *next; }; typedef struct node NODE; NODE *first,*current,*previous; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.192.90.247

12/20 17:07, , 1F
利用置底文的網站將你編譯失敗的程式碼po上來
12/20 17:07, 1F
※ 編輯: jacky1989 來自: 123.192.90.247 (12/20 17:12)

12/20 17:16, , 2F
怎麼會用int initial()回傳NODE*呢...
12/20 17:16, 2F

12/20 17:17, , 3F
那我應該怎麼修改才對??
12/20 17:17, 3F

12/20 17:17, , 4F
從來沒有這樣傳遞過,也不知道型態要怎麼定義
12/20 17:17, 4F

12/20 17:19, , 5F
如果first要宣告在.h的話,加個extern修飾符就可以了
12/20 17:19, 5F

12/20 17:20, , 6F
這樣也不用回傳,其它function就可以看到那個first
12/20 17:20, 6F
文章代碼(AID): #1Ey4_tPA (C_and_CPP)