-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstMissingPositive.c
98 lines (91 loc) · 2.61 KB
/
FirstMissingPositive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
struct anode {
int key;
int value;
struct anode *next;
};
struct hash {
int size;
struct anode **table;
};
struct hash make_table(int s) {
// Make the struct which will be returned
struct hash new;
// Seet the size to s
new.size = s;
// Malloc enough space for the table
struct anode **list = malloc(sizeof(struct anode*) * s);
new.table = list;
for (int i = 0; i < s; ++i) {
// Malloc an anode for each element of the array
struct anode *newlist = malloc(sizeof(struct anode));
// Set each next of the newlist to NULL;
// These anodes will act as a buffer, which makes coding functions simple
(*newlist).next = NULL;
// Set each element to these newlists
new.table[i] = newlist;
}
return new;
}
int *search(struct hash T, int k) {
// Find the module which will determine which one of the linked list
int modu = k % T.size;
// pointer to the first element of the selected linked list
// Rememebr this is the buffer anode
struct anode *pointer = T.table[modu];
// While there are more elements inside of the anode,
while((*pointer).next != NULL) {
if(k == (*pointer).key) {
return (*pointer).value;
}
pointer = (*pointer).next;
}
// Lastly, check the last element
if (k == (*pointer).key) {
return (*pointer).value;
}
return NULL;
}
void add(struct hash T, int k) {
int modu = k % T.size;
struct anode *new = malloc(sizeof(struct anode));
(*new).value = k;
(*new).next = (*T.table[modu]).next;
(*new).key = k;
(*(T.table)[modu]).next = new;
}
void delete(struct hash T, int k) {
int modu = k % T.size;
struct anode *prev = NULL;
struct anode *pointer = T.table[modu];
while((*pointer).next != NULL) {
if(k == (*pointer).key) {
struct anode *temp = pointer;
pointer = (*pointer).next;
(*prev).next = pointer;
free((*temp).value);
free(temp);
}
else {
prev = pointer;
pointer = (*pointer).next;
}
}
if (k == (*pointer).key) {
(*prev).next = NULL;
free((*pointer).value);
free(pointer);
}
}
int firstMissingPositive(int* nums, int numsSize){
struct hash t = make_table(10000);
for (int i = 0; i < numsSize; ++i) {
if (nums[i] > 0 && search(t, nums[i]) == NULL) add(t, nums[i]);
}
int counter = 1;
while(search(t, counter) != NULL) ++counter;
return counter;
}