#include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char *City; int Temp; struct Node *Next; }; typedef struct Node * Link; /*Links are pointers to nodes*/ Link Head; /*head of our linked list*/ int NodeCount; /*how many nodes in the list*/ /*---functions declarations for linked list---*/ int AddNodeAscend(Link); /* add a node*/ void CreateList(void); int DeleteNode(Link); int DuplicateNode(Link,Link); void FreeNode(Link); void ShowNodes(void); int NodeCmp(Link,Link); /*---function definitions---*/ int AddNodeAscend(Link to_add) { Link pn, /* local copy of node to be added */ prev, /* points to previous node*/ curr; /* points to node being examined*/ struct Node dummy; int i; /*Make a copy of the input node*/ pn=(Link)malloc(sizeof(struct Node)); if (pn==NULL) return 0; memcpy(pn,to_add,sizeof(struct Node)); /* set up a dummy node to simplify logic */ dummy.Next=Head; prev=&dummy; curr=Head; /*insert node pn*/ for (;;prev=curr,curr=curr->Next) { if (curr==NULL) break; i=NodeCmp(pn,curr); if (i<=0) break; /*pn precedes curr*/ } if(curr&& i==0) /* we have duplicate */ if (DuplicateNode(curr,pn)==0) return (1); /* bail out if DuplicatieNode says to*/ prev->Next=pn; pn->Next=curr; Head=dummy.Next; return (1); } /* Handle the duplicate node. In this program, we just delete the dupliacte */ int DuplicateNode(Link inList,Link dupliacte) { FreeNode(dupliacte); return (0); } int DeleteNode(Link to_delete) { Link curr, prev; int i; /*--- Is there anything in the list---*/ if (Head==NULL) { return (0); } /*---if so,step through the list looking for the node ---*/ for (prev=NULL,curr=Head;curr!=NULL && (i=NodeCmp(to_delete,curr))>0;prev=curr,curr=curr->Next) /*loop around*/; /*--- Found a match,so delete it---*/ if (curr!=NULL && i==0) { if (prev) prev->Next=curr->Next; else /* delete Head*/ Head=curr->Next; FreeNode(curr); NodeCount-=1; } return (1); } int NodeCmp(Link a,Link b) { if (a->Temp!=b->Temp) return (a->Temp-b->Temp); 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; 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"); } } /*--- main line ---*/ int main(int argc ,char*argv[]) { FILE * fin; /*file we'll be read from*/ char buffer[128]; 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); } /*Create and initialize the linked list to empty*/ CreateList(); while(!feof(fin)) { /* read a record consisting of a line of text*/ if (fgets(buffer,127,fin)==NULL) break; /* get rid of the trailing carriage return */ buffer[strlen(buffer)-1]='/0'; /* copy the city name to the node to be added*/ n.City=strdup(buffer+3); /*mark of the temperature and convert to int*/ buffer[3]='/0'; n.Temp=atoi(buffer); /*add the node to the list*/ if (AddNodeAscend(&n )==0) { fprintf(stderr,"Error add node. Aborting/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); }
有个命令行参数
写的是文件路径
文件里面的内容格式
-10Boise, ID
-05Missoula,Mt