#include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX 100
typedef struct node { int count; char name[20]; struct node* next; }NODE; NODE* find(NODE* head,char* name) { NODE* p = head->next; while(p!=NULL) { if(strcmp(p->name,name) == 0) break; else p = p->next; } return p; }
int list_print(NODE* head) { NODE* p = head->next; while(p!=NULL) { printf("%s\t%d\n",p->name,p->count); p = p->next; } return 0; }
int my_free(NODE* head) { NODE* p = head; NODE* q = NULL; while(p!=NULL) { q = p; p = p->next; free(q); } return 0; } int main(int argc, char *argv[]) {
FILE* fp = fopen("url.txt","r"); if( fp==NULL ) { printf("fopen error\n"); return -1; } char* buf = (char*)malloc(100); if(buf == NULL) { printf("malloc error\n"); return -1; } memset(buf,0,100); NODE* head = (NODE*)malloc(sizeof(NODE)); if(head == NULL) { printf("malloc error\n"); return -1; } head->next = NULL; NODE* p = head; while(fgets(buf,100,fp)!=NULL) { sprintf(buf,"%s",strrchr(buf,'/')); buf++; if(*buf=='\0') { sprintf(buf,"%s","none file name"); // continue;
} else { char* tmp = buf; while(*tmp != '\0') { if(*tmp == '.') *tmp = '\0'; else tmp++; } } printf("%s\n",buf); NODE* tmp = find(head,buf); if(tmp == NULL) { NODE* q = (NODE*)malloc(sizeof(NODE)); if(q == NULL) { printf("malloc error\n"); return -1; } q->count = 1; sprintf(q->name,"%s",buf); q->next = p->next; p->next = q; p = q; //list_print(head);
} else { (tmp->count)++; } } list_print(head); my_free(head); free(buf); buf = NULL; system("PAUSE"); return 0; }
|