哈希表 - 使用qsort对结构进行排序

时间:2014-12-25 14:15:11

标签: c sorting hashtable

很抱歉创造了另一个问题,但最后一个问题变得不堪重负。 所以我正在制作一个哈希表,它插入文件中的单词(标记),在插入它们之后我需要对它们进行排序。给出了程序模板,唯一没有完成的功能是:insert_ht(),clear_ht()和compare。尽管我通过比较对qsort进行了大量搜索,但程序并没有对频率进行排序(每个单词的插入次数)。我希望他们从最高到最低排序。

以下是代码:"请注意,我不应更改insert_ht() , clear_ht() and compare以外的任何功能



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define HTABLE_SIZ 1001
#define MAX_LINE_SIZ 1024

/* Hash Table */
typedef struct node* link;
struct node { char *token; int freq; link next; };

link htable[HTABLE_SIZ] = { NULL }; /* Table of lists (#buckets) */
int size = 0; /* Size (number of elements) of hash table */

unsigned int hash (char *tok );
void insert_ht (char *data);
void clear_ht ( );
void print_ht ( );

void Process(FILE *fp);


int main(int argc, char *argv[])
{
    int i;
    FILE *fp;
    printf("prin tin for \n");
    for (i=1; i < argc; i++)
    {
        printf("prin tin fopen \n");
        fp = fopen(argv[i],"r");
        if (NULL == fp)
        {
            fprintf(stderr,"Problem opening file: %s\n",argv[i]);
            continue;
        }
        printf("prin tin process \n");
    Process(fp);
    fclose(fp);
    }
    print_ht();
    //clear_ht();
    return 0;
}


void Process(FILE *fp)
{
    const char *seperators = " ?!'\";,.:+-*&%(){}[]<>\\\t\n";

    char line[MAX_LINE_SIZ];
    char *s;
    while((fgets(line,MAX_LINE_SIZ, fp)) != NULL)
    {
        for (s=strtok(line,seperators); s; s=strtok(NULL,seperators)){
            printf("prin tin insert %s \n",s);
            insert_ht(s);
        }
            
        }
    }
    
/* Hash Function */
unsigned int hash(char *tok)
{
    printf("bike stin hash \n");
    unsigned int hv = 0;
    while (*tok)
        hv = (hv << 4) | toupper(*tok++);
    printf("VGAINEIIIIIIIIIIIIII %d \n",hv);
    return hv % HTABLE_SIZ;
}



void insert_ht(char *token)
{
    printf("bike stin insert %s \n",token);
    unsigned int hashval = hash(token);
    struct node *new_list;

    if (htable[hashval]==NULL){
        printf("mesa stin prwti if %u %s \n",hashval,token);
        //token = strdup(token);
        new_list = malloc(sizeof(link));
        new_list->token = strdup(token) ;
        new_list->freq = 1;
        new_list->next = htable[hashval];
        htable[hashval] = new_list;
        size++;
        
    }else {
        htable[hashval]->freq++;
    }
    printf("ta evale epitixws \n");
    
}

void clear_ht()
{
    int i;
    
    
    
    
    for(i=0;  i<HTABLE_SIZ; i++) {
        
        while(htable[i]->token!=NULL) {
            
            htable[i]->token=NULL;
            htable[i]->freq=NULL;
            free(htable[i]);
        }
    }

  
}


int compare(const void *elem1, const void *elem2)
{
    const struct node *p1 = elem1;
    const struct node *p2 = elem2;
    if (p1->freq > p2->freq)
        return(+1);
    else if (p1->freq < p2->freq)
        return(-1);
    else
        return(0);
}
void print_ht()
{
    int i, j=0;
    link l, *vector = (link*) malloc(sizeof(link)*size);
    
    for (i=0; i < HTABLE_SIZ; i++)
        for (l=htable[i]; l; l=l->next)
            vector[j++] = l;
        
    qsort(vector,size,sizeof(link),compare);
    for (i=0; i < size; i++)
        printf("%-50s\t%7d\n",vector[i]->token,vector[i]->freq);
    free(vector);
} 
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

Ι找到了解决方案。显然由于某些原因我的compare功能是错误的。 我仍然没有弄清楚为什么,但这里是正确的,希望其他人会发现这篇文章有用!

int compare(const void *elem1, const void *elem2)
{
    
     return (*(link*)elem2)->freq - (*(link*)elem1)->freq;
}