链表 的 增删改查(源码分析)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Node
- {
- char *City;
- int Temp;
- struct Node *Next;
- };
- typedef struct Node *Link;
- Link Head;
- int NodeCount;
- int AddNodeAscend(Link);
- int DuplicateNode(Link,Link);
- void CreateList(void);
- int DeleteNode(Link);
- void FreeNode(Link);
- void ShowNodes(void);
- int NodeCmp(Link,Link);
- /* -----function definition*/
- int AddNodeAscend(Link to_add)
- {
- Link pn,
- prev,
- curr;
- struct Node dummy;
- int i;
- pn = (Link)malloc(sizeof(struct Node));
- if(NULL== pn)
- {
- return 0;
- }
- memcpy(pn,to_add,sizeof(struct Node));
- /*--set up a dummy node to simply logic*/
- dummy.Next = Head;
- prev = &dummy;
- curr = Head;
- /* ---insert node pn--*/
- for( ; ;prev = curr,curr = curr->Next)
- {
- if(NULL == curr)
- break;
- i = NodeCmp(pn,curr);
- if(i<=0)
- break;
- }
- if(0==curr && i)
- if(DuplicateNode(curr,pn)==0)
- return (1);
- prev->Next = pn;
- pn->Next = curr;
- Head = dummy.Next;
- return (1);
- }
- int DuplicateNode(Link inlist,Link duplicate)
- {
- FreeNode(duplicate);
- return (0);
- }
- int DeleteNode(Link to_delete)
- {
- Link curr,
- prev;
- int i;
- /*Is there anything in the list?----*/
- if(NULL == Head)
- return (0);
- for(prev = NULL,curr = Head;
- curr !=NULL&&(i=NodeCmp(to_delete,curr))>0;
- prev =curr,curr = curr->Next)
- /*Find a match ,so delete it ---*/
- if(curr !=NULL&&i==0)
- {
- if(prev)
- prev->Next = curr->Next;
- else
- Head = curr->Next;
- FreeNode(curr);
- NodeCount -=1;
- return (1);
- }
- return (0);
- }
- int NodeCmp(Link a,Link b)
- {
- /*returns 1,0,-1 ,depending on whether
- *the data in a is
- *greater,equal,or less than b
- *
- */
- if(a->Temp!=b->Temp)
- return (a->Temp-b->Temp);
- else
- return strcmp(a->City,b->City);
- }
- void CreateList(void)
- {
- Head =NULL;
- NodeCount = 0;
- }
- void FreeNode(Link n)
- {
- free(n->City);
- free(n);
- }
- void ShowNodes(void)
- {
- Link pn;
- int count,median;
- for(count=0,pn=Head; pn; pn= pn->Next)
- count+=1;
- //compute the median Node;
- median =count/2+1;
- if(count)
- {
- count = 0;
- for(pn=Head; pn ; pn=pn->Next)
- {
- printf("%-20s:%3d",pn->City,pn->Temp);
- count+=1;
- if(count == median)
- printf("---Median---");
- printf("/n");
- }
- }
- else
- {
- printf("Empty list /n");
- }
- }
- int main(int argc,char *argv[])
- {
- FILE *fin;
- char buffer[120];
- struct Node n;
- if(argc !=2)
- {
- fprintf(stderr,"Usage:citytemp filename.ext/n");
- exit(EXIT_FAILURE);
- }
- fin = fopen(argv[1],"rt");
- if(fin == NULL)
- {
- fprintf(stderr,"Cannot open/find %s/n",argv[2]);
- exit(EXIT_FAILURE);
- }
- while (!feof(fin))
- {
- if(fgets(buffer,127,fin)==NULL)
- break;
- buffer[strlen(buffer-1)]='/0';
- n.City = strdup(buffer+3);
- buffer[3] = '/0';
- n.Temp = atoi(buffer);
- if(AddNodeAscend(&n)==0)
- {
- fprintf(stderr,"Error adding node.abborting/n");
- exit(EXIT_FAILURE);
- }
- }
- ShowNodes();
- printf("/n");
- DeleteNode(Head);
- ShowNodes();
- while(Head && Head->Next)
- {
- printf("/n");
- DeleteNode(Head->Next);
- ShowNodes();
- }
- printf("/n");
- DeleteNode(Head);
- ShowNodes();
- fclose(fin);
- return (EXIT_SUCCESS);
- }