---
title: "动态链表"
author: "gauss2008"
date: "2006-11-10T10:48:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2006-11-10T10:48:00Z"
id: 74710
url: "https://gauss2008.iteye.com/blog/74710"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;stdio.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include &lt;malloc.h&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)typedef struct LNode  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    FILE \*fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    char \*fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    struct LNode \*next;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}LNode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)LNode \*LinkList;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)LNode \*ListInit(LNode \*linklist)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if((linklist=(LNode \*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("Init Link Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    linklist-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)ListInsert(LNode \*linklist, char \*fname,FILE \*fp)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    LNode \*lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  LNode \*p;    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  char \*test;    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  p=linklist;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif) test=fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)printf("in list insert fname=%s ",fname);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)            if((lnode=(LNode\*)malloc(sizeof(LNode)))==NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    printf("Insert Error");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    exit(0);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                while(p-&gt;next!=NULL)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    p=p-&gt;next;                      
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                    if(!strcmp(p-&gt;fname,fname))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            p-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                            return ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p-&gt;next=lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fp=fp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;fname=fname;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //strcpy(lnode-&gt;fname,fname);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                lnode-&gt;next=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                p=lnode;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                printf("...........test1=%s ",test);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                //test="AAA";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)                while(\*test!='