ranking system

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.4 kB
4
Indexable
Never
#include <stdio.h>
//#include "function.h"
#include <string.h>
#include <stdlib.h>

#define MAX_SIZE 1000
#define MAX_LEN 100

typedef struct{
    int score;
    char* name;
} Node;

Node* Table[MAX_SIZE];
// N = number of nodes in Table
 
void Insert( Node** Table, int N, int score, char* name ){
    Table[N] = (Node*)malloc(sizeof(Node)*1);
    Table[N]->name = (char*)malloc(sizeof(char)*(strlen(name)+1));
    strcpy(Table[N]->name, name);
    Table[N]->score = score;
}
void Delete( Node** Table, int N, char* name ){
    for(int i = 0; i < N; i++){
        if(strcmp(Table[i]->name, name) == 0){
			free(Table[i]->name);
            free(Table[i]);
            for(int j = i; j < N-1; j++){
                Table[j] = Table[j+1];
            }
            Table[N-1] = NULL;
            //free(Table[N-1]->name);
            //free(Table[N-1]);
            return;
        }
    }
}
int cmp(const void *a, const void *b){
    if(Table[*(int*)a]->score < Table[*(int*)b]->score) return 1;
    else if(Table[*(int*)a]->score == Table[*(int*)b]->score){
        if(strcmp(Table[*(int*)a]->name, Table[*(int*)b]->name) > 0) return 1;
        else return -1;
    }
    else return -1;
}
int* Top( Node** Table, int N, int x){
    int *ans = (int*)malloc(sizeof(int)*N);
    for(int i = 0; i < N; i++){
        ans[i] = i;
    }
    qsort(ans, N, sizeof(int), cmp);
    return ans;
}