[問題] Linkedlist用法
開發平台(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,¤t->name,¤t->nickname,¤t->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
12/20 17:07, 1F
※ 編輯: jacky1989 來自: 123.192.90.247 (12/20 17:12)
→
12/20 17:16, , 2F
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
12/20 17:19, 5F
→
12/20 17:20, , 6F
12/20 17:20, 6F