---
# System prepended metadata

title: 13077 - Ranking System
tags: [dynamic memory]

---

# 13077 - Ranking System
>author: Utin
###### tags: `dynamic memory`

---

## Brief
See the code below

## Solution 0

```c=
#ifndef __FUNCTION_H__
#define __FUNCTION_H__

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

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

void Insert(Node** Table, int N, int score, char* name);
void Delete(Node** Table, int N, char* name);
int* Top(Node** Table, int N, int x);

int cmp(Node* A, Node* B) {
    if (A->score != B->score) return B->score - A->score;
    return strcmp(A->name, B->name);
}

void Insert(Node** Table, int N, int score, char* name) {
    Table[N] = (Node*) malloc(sizeof(Node));
    Table[N]->score = score;
    Table[N]->name = (char*) malloc(101);
    strcpy(Table[N]->name, name);
    for (int i = N; i > 0; i--) {
        int status = cmp(Table[i-1], Table[i]);
        if (status > 0) {
            Node* temp = Table[i-1];
            Table[i-1] = Table[i];
            Table[i] = temp;
        }
        else break;
    }
}

void Delete(Node** Table, int N, char* name) {
    for (int i = 0; i < N; i++) {
        if (!strcmp(Table[i]->name, name)) {
            Node* trash = Table[i];
            for (int j = i+1; j < N; j++) {
                Table[j-1] = Table[j];
            }
            Table[N-1] = NULL; // 第四筆沒過記得加這行
            free(trash->name); // 記得free不然會MLE
            free(trash);       // 記得free不然會MLE
            break;
        }
    }
}

int* Top(Node** Table, int N, int x) {
    int* arr = (int*) malloc(sizeof(int) * x);
    for (int i = 0; i < x; i++) arr[i] = i;
    return arr;
}

#endif
// By Utin
```

## Reference