/************************************ * * Include File * *************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /************************************ * * Data Definitions * *************************************/ struct Node { char* City; int Temp; struct Node* Next; }; typedef struct Node* Link; Link Head = NULL; int NodeCount = 0; /************************************ * * Functions Declarations * *************************************/ /** Initialize List **/ void CreateList(void); /** Add Node **/ int AddNodeAscend(Link); /**Delete Node **/ int DeleteNode(Link); /** Free Node **/ void FreeNode(Link); /** Handle Duplication Node **/ int DuplicationNode(Link,Link); /** Show List Of Nodes **/ void ShowNode(void); /** Compare Two Node**/ int NodeCmp(Link,Link); /************************************ * * Functions Definitions * *************************************/ int AddNodeAscend( Link to_add) { Link pn, /* local copy of node to be add */ prev, /* points to previous node */ curr; /* points to node being examined */ struct Node dummy; int i ; pn = (Link)malloc(sizeof(struct Node)); if(pn == NULL) return 0; memcpy(pn,to_add,sizeof(struct Node)); dummy.Next =Head; prev= &dummy; curr = Head; for(;;prev = curr,curr= curr->Next) { if(curr = NULL) break; i = NodeCmp(pn, curr); if(i <= 0) break; } if(curr && i == 0) if(DuplicationNode(curr, pn)) return(1); prev->Next = pn; pn->Next = curr; Head = dummy.Next; NodeCount += 1; return(1); } int DuplicationNode( Link inlist, Link duplication) { FreeNode(duplication); return (0); } int DeleteNode( Link to_delete) { Link curr, prev; int i ; if(Head = NULL) return(0); for( prev = NULL,curr = Head; curr != NULL &&(i = NodeCmp(to_delete, curr))>0; prev = curr,curr = curr->Next); 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) { 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 ShowNode( 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(" -----Midian ------"); printf("\n"); } } else printf("Empty List !\n"); } int main( int argc, char* argv[]) { FILE* fin; 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 ,"Cann't open file %s\n ",argv[2]); exit(EXIT_FAILURE); } CreateList(); 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 . Aborting !\n"); exit(EXIT_FAILURE); } } ShowNode(); printf("\n"); DeleteNode(Head); ShowNode(); while(Head &&Head ->Next) { printf("\n"); DeleteNode(Head ->Next); ShowNode(); } printf("\n"); DeleteNode(Head); ShowNode(); fclose(fin); return (EXIT_SUCCESS); }