BuringStraw

BuringStraw

Post a hash table code.

Problem Link:
https://www.luogu.com.cn/problem/P4305

#include <stdio.h>
#include <stdlib.h>
#define MAXN 50005//Data Range
#define MOD 400000//Modulus, not too large, otherwise it will exceed memory
int a[MAXN];//Used to save the result

struct node
{
    struct node* next;//Pointer to the next item
    int value;//The value saved in this node
};

struct node* head[MOD];//Hash table, composed of a bunch of linked lists, storing the head pointers of the linked lists

void insert(struct node* p, int v)//Insert v after node p
{
    if (p == NULL) return;//Cannot operate on NULL, for safety
    struct node *t = malloc(sizeof(struct node));//Allocate memory for the new node
    t -> value = v;//Set the value
    //An insertion process
    t -> next = p -> next;//Set the next item of this item to the next item of the previous item
    p -> next = t;//Set the next item of the previous item to this item
}

int find(struct node*p, int v)//Search from node p to the end of the linked list until found or reached the end of the list
{
    while(p != NULL)//Stop at NULL, the next of the last item is set to NULL by default
    {
        if (p -> value == v)//Found v
        {
            return 1;//Exit the loop directly if found
        }
        p = p -> next;//Jump forward
    }
    return 0;//Only have the opportunity to end the loop if not found
}

int main() {
    int t;//Number of data sets
    scanf("%d", &t);
    while (t--)//Loop only t times
    {
        for (int i = 0; i < MOD; ++i)
        {
            head[i] = NULL;
        }
        int n;
        scanf("%d", &n);
        int newp = 0;//Record the index of the latest element in a, since we use 1 as the index of the first element for personal habits
        for (int i = 1; i <= n; ++i)
        {
            int x;
            scanf("%d", &x);//Enter a number
            int flag = 0;//0 means no duplicate data, 1 means yes
            int m = x % MOD;//The result after modulus, used for indexing in head
            if (m < 0)//Negative numbers modulo negative numbers, causing the index to go beyond
                m = -m;
            //There are three cases when searching
            if (head[m] == NULL)//The linked list pointed to by head[m] does not exist
            {//Manually initialize the first element
                head[m] = malloc(sizeof(struct node));//Allocate memory
                head[m] -> value = x;//Set the value
                head[m] -> next = NULL;//There is no next item yet
            } else
            {//The linked list exists
                if (find(head[m], x)/*Search for x*/) flag = 1;//Found
                else
                {//Not found
                    struct node* p = head[m];//Get the head pointer of the linked list
                    while (p -> next != NULL) p = p -> next;//Go to the end
                    insert(p, x);//Insert
                }
            }
            if (!flag) a[++newp] = x;//Okay, we didn't find x, we can insert a new element into a
        }

        for (int i = 1; i <= newp; ++i)//Output...
        {
            printf("%d ", a[i]);
        }
        putchar('\n');
    }
    return 0;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.