user-0 提问: 大家来找茬，一道链表基础题

回复:
user: user-1
content: 粗略写了份，测试OK。 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h>   #define PRINT(msg)	print(msg)  void print(const char *msg) { 	printf("%s[%d]:%s\r\n", __FUNCTION__, __LINE__, msg); }  typedef struct _LinkList { 	int data; 	struct _LinkList *pnext; }Linklist;   int main(void) { 	int i = 0; 	int data = 0; 	Linklist *first_file = NULL, *last_file = NULL; 	Linklist *file = NULL;  	srand(time(NULL));  	while(i++ < 10) 	{ 		data = (rand() % 1000 + i)/i;  		file = (Linklist *)malloc(sizeof(Linklist)); 		if(file == NULL) 		{ 			PRINT("alloc memory failed.\n"); 		}  		file->data = data; 		file->pnext = NULL;  		if(first_file == NULL) 		{ 			first_file = file; 		}  		if(last_file == NULL) 		{ 			last_file = file; 		} 		else 		{ 			last_file->pnext = file; 			last_file = file; 		} 	} 	 	while(first_file != NULL) 	{ 		printf("%d ", first_file->data); 		first_file = first_file->pnext; 	} 	printf("\n");  }

user: user-2
content: #include "stdafx.h" #include "malloc.h" #include "string" #include <iostream>  typedef struct FileList{ 	int num; 	struct FileList *pnext; }FileList;  int main() { 	FileList *first_file; 	FileList *last_file,*file; 	int num;  	first_file = NULL; 	last_file = first_file;  	while(true) 	{ 		printf("please input num\n"); 		scanf("%d",&num);  		if (num<0) 		{ 			printf("quit"); 			break; 		}  		file = (FileList *)malloc(sizeof(FileList)); 		memset(file,0,sizeof(FileList)); 		file->num=num;		  // 		last_file=file; // 		last_file=file->pnext; 		if (first_file == NULL) //第一次计入数据 		{ 			file->pnext = last_file; 			last_file=file; 			first_file =file; 		} 		else 		{ 			file->pnext =first_file; 			first_file=file; 		 		} 	}   	 	FileList *temp_file = first_file;     //打印数据 	while(temp_file != NULL) 	{ 		std::cout<<temp_file->num;		 		temp_file = temp_file->pnext;		 	}  	//释放内存     temp_file = first_file; 	while(first_file != NULL) 	{ 		temp_file =first_file; 		first_file = first_file->pnext; 		delete  temp_file ; 		temp_file = NULL; 	}  	getchar(); 	getchar(); 	return 0; }

user: user-3
content: last_file=file;       last_file=file->pnext; 改成       last_file ? (last_file->pnext = file) : (first_file = file);       last_file=file;

user: user-4
content: 链表没有建立起来，取自己的next，赋值给自己，你很好玩哈！没事“自己搞自己”，这个难度系数有点高哈 其次，内存泄漏 最后，不是病的病，头文件包含标准库的话，用《》得了，别让编译器再到本地目录查了！  至于修改，楼上给出一些方案了，我再给出另外一种解决方式，头插入法 这句  last_file=file; last_file=file->pnext;  替换为下面句  file->pnext = first_file; first_file = file;  over

user: user-5
content: //将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h> struct FB {     char fn[256];     size_t fl;     char *b;     struct FB *next;     struct FB *prev; } *fh,*fb,*ft; char ln[256]; char fpn[256]; FILE *af; FILE *f; int L,n; int main() {     system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");     af=fopen("c:\\allfn.txt","r");     if (NULL==af) {         printf("Can not open file c:\\allfn.txt!\n");         return 1;     }     fh=NULL;     fb=NULL;     n=0;     while (1) {         if (NULL==fgets(ln,256,af)) break;         L=strlen(ln);         if ('\n'==ln[L-1]) ln[L-1]=0;         printf("read %s\n",ln);         strcpy(fpn,"c:\\tmp\\");         strcat(fpn,ln);         ft=(struct FB *)malloc(sizeof(struct FB));         if (NULL==ft) {             printf("Can not malloc ft!\n");             fclose(af);             return 2;//之前的malloc在main退出后由操作系统自动free         }         printf("ft[%d]==%p\n",n,ft);         strcpy(ft->fn,fpn);         f=fopen(fpn,"rb");         if (NULL==f) {             printf("Can not open file %s!\n",fpn);             fclose(af);             return 3;//之前的malloc在main退出后由操作系统自动free         }         ft->fl=_filelength(fileno(f));         ft->b=malloc(ft->fl);         if (NULL==ft->b) {             printf("Can not malloc ft->b!\n");             fclose(f);             fclose(af);             return 4;//之前的malloc在main退出后由操作系统自动free         }         printf("ft[%d]->b==%p\n",n,ft->b);         if (ft->fl!=fread(ft->b,1,ft->fl,f)) {             printf("fread error!\n");             fclose(f);             fclose(af);             return 5;//之前的malloc在main退出后由操作系统自动free         }         fclose(f);         ft->next=NULL;          if (NULL==fh) {             ft->prev=NULL;             fh=ft;         } else {             fb->next=ft;             ft->prev=fb;         }         fb=ft;         n++;     }     fclose(af);     printf("-----list-----\n");     for (ft=fh;NULL!=ft;ft=ft->next) {         printf("%8d %s\n",ft->fl,ft->fn);         if (NULL!=ft) fb=ft;     }     printf("-----free-----\n");     n--;     if (NULL!=fh) {         for (ft=fb->prev;NULL!=ft;ft=ft->prev) {             if (NULL!=ft->next->b) {                 printf("ft[%d]->b==%p\n",n,ft->next->b);                 free(ft->next->b);             }             if (NULL!=ft->next) {                 printf("ft[%d]==%p\n",n,ft->next);                 free(ft->next);             }             n--;         }         if (NULL!=fh->b) {             printf("ft[0]->b==%p\n",fh->b);             free(fh->b);         }         printf("ft[0]==%p\n",fh);         free(fh);     }     return 0; } //C:\tmp\tmp\Debug>dir /a-d c:\tmp // 驱动器 C 中的卷是 C_HD5_1 // 卷的序列号是 1817-D526 // // c:\tmp 的目录 // //找不到文件 // //C:\tmp\tmp\Debug>tmp //找不到文件 //-----list----- //-----free----- // //C:\tmp\tmp\Debug>dir /a-d c:\tmp // 驱动器 C 中的卷是 C_HD5_1 // 卷的序列号是 1817-D526 // // c:\tmp 的目录 // //2011-06-30  18:04            44,840 my_c.rar //2011-06-30  17:18             1,036 err.frm //2011-06-30  14:32            14,243 出租.txt //2011-06-28  12:08            23,681 MSDN98书签.txt //             4 个文件         83,800 字节 //             0 个目录 17,041,870,848 可用字节 // //C:\tmp\tmp\Debug>tmp //read my_c.rar //ft[0]==00421800 //ft[0]->b==00520068 //read err.frm //ft[1]==00421670 //ft[1]->b==0052AFC0 //read 出租.txt //ft[2]==00421530 //ft[2]->b==00378F28 //read MSDN98书签.txt //ft[3]==004213F0 //ft[3]->b==0052B3F8 //-----list----- // 44840 c:\tmp\my_c.rar //  1036 c:\tmp\err.frm // 14243 c:\tmp\出租.txt // 23681 c:\tmp\MSDN98书签.txt //-----free----- //ft[3]->b==0052B3F8 //ft[3]==004213F0 //ft[2]->b==00378F28 //ft[2]==00421530 //ft[1]->b==0052AFC0 //ft[1]==00421670 //ft[0]->b==00520068 //ft[0]==00421800 // //C:\tmp\tmp\Debug>

user: user-6
content: #include <stdio.h> #include <stdlib.h> #include <string.h>  typedef struct FileList{     int num;     struct FileList *pnext; }FileList;   void walk(FileList *head) {     FileList *file;      for (file = head; file; file = file->pnext) {         printf("%p:%d\n", file, file->num);     } }  int main() {     FileList *first_file;     FileList *last_file,*file;     int num;       first_file = NULL;     last_file = first_file;       while(1)     {       printf("please input num\n");       scanf("%d",&num);         if (num<0)       {           break;           printf("quit");           return -1;       }         file = (FileList *)malloc(sizeof(FileList));       memset(file,0,sizeof(FileList));       file->num=num;         /*       last_file=file;       last_file=file->pnext;       */       if (first_file == NULL) {           first_file = last_file = file;       } else {         last_file->pnext = file;         last_file = file;       }     }      walk(first_file);      return 0; }

user: user-7
content: file->pnext = last_file; last_file = file;

user: user-7
content: 引用 13 楼 lyyiqo 的回复:最后两句互掉.............cuo le

user: user-7
content: 最后两句互掉

user: user-0
content: 1、最好不要添加删除语句 ======================

user: user-8
content: 第34、35行改成：  if (last_file != NULL) {     last_file->pnext = file; } last_file = file;

user: user-9
content: 一看就发现少了这一句 #include"stdio.h"

user: user-10
content: 参考代码： http://pan.baidu.com/s/17yIcQ

user: user-8
content: 引用 7 楼 smartjeckk 的回复:不要意思，我重新声明一下 1、最好不要添加删除语句 2、FileList *first_file为头结点 3、上面的修改没有成功的，运行有错误或者链表没有连接起来 第34、35行对应下面这两行：        first_file = first_file ? first_file : file;       last_file = last_file ? (last_file->pnext = file) : file;

user: user-0
content: 不要意思，我重新声明一下 1、最好不要添加删除语句 2、FileList *first_file为头结点 3、上面的修改没有成功的，运行有错误或者链表没有连接起来

user: user-11
content: 上面的代码中对file初始化的的memset可以去掉了

user: user-11
content: 去掉对空指针各种作死的引用，去掉定义指针后奇怪的误导初始化  typedef struct FileList{     int num;     struct FileList *pnext; }FileList;   int main() {     FileList *first_file=NULL;     FileList *last_file=NULL,*file=NULL;     int num;       while(1)     {       printf("please input num\n");       scanf("%d",&num);         if (num<0)       {           printf("quit");           return -1;       }         file = (FileList *)malloc(sizeof(FileList));              if (NULL!=file)       {         memset(file,0,sizeof(FileList));         file->num=num;         file->pnext=NULL;  		if (NULL == first_file)  		{  			first_file=file;   			last_file =file;  	    }  	    else  	    {  	    	last_file->pnext = file;  	    }  	    last_file=last_file->pnext;   	  }   	  else       {     	printf("malloc failed\n")     	return 1;       }     }         return 0; }

user: user-12
content: 应该给出错误或者其他不合理现象之类的吧

user: user-13
content: 34,35改为下边的： file->pnext = NULL; if(first_file == NULL) {     first_file = file; } last_file = file;