tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct list_node {int x; struct list_node *next;};
|
||||
typedef struct list_node node;
|
||||
|
||||
node * uniq(int *a, unsigned alen)
|
||||
{if (alen == 0) return NULL;
|
||||
node *start = malloc(sizeof(node));
|
||||
if (start == NULL) exit(EXIT_FAILURE);
|
||||
start->x = a[0];
|
||||
start->next = NULL;
|
||||
|
||||
for (int i = 1 ; i < alen ; ++i)
|
||||
{node *n = start;
|
||||
for (;; n = n->next)
|
||||
{if (a[i] == n->x) break;
|
||||
if (n->next == NULL)
|
||||
{n->next = malloc(sizeof(node));
|
||||
n = n->next;
|
||||
if (n == NULL) exit(EXIT_FAILURE);
|
||||
n->x = a[i];
|
||||
n->next = NULL;
|
||||
break;}}}
|
||||
|
||||
return start;}
|
||||
|
||||
int main(void)
|
||||
{int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
|
||||
for (node *n = uniq(a, 10) ; n != NULL ; n = n->next)
|
||||
printf("%d ", n->x);
|
||||
puts("");
|
||||
return 0;}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Returns `true' if element `e' is in array `a'. Otherwise, returns `false'.
|
||||
* Checks only the first `n' elements. Pure, O(n).
|
||||
*/
|
||||
bool elem(int *a, size_t n, int e)
|
||||
{
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
if (a[i] == e)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Removes the duplicates in array `a' of given length `n'. Returns the number
|
||||
* of unique elements. In-place, order preserving, O(n ^ 2).
|
||||
*/
|
||||
size_t nub(int *a, size_t n)
|
||||
{
|
||||
size_t m = 0;
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
if (!elem(a, m, a[i]))
|
||||
a[m++] = a[i];
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/* Out-place version of `nub'. Pure, order preserving, alloc < n * sizeof(int)
|
||||
* bytes, O(n ^ 2).
|
||||
*/
|
||||
size_t nub_new(int **b, int *a, size_t n)
|
||||
{
|
||||
int *c = malloc(n * sizeof(int));
|
||||
memcpy(c, a, n * sizeof(int));
|
||||
int m = nub(c, n);
|
||||
*b = malloc(m * sizeof(int));
|
||||
memcpy(*b, c, m * sizeof(int));
|
||||
free(c);
|
||||
return m;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
|
||||
int *b;
|
||||
|
||||
size_t n = nub_new(&b, a, sizeof(a) / sizeof(a[0]));
|
||||
|
||||
for (size_t i = 0; i < n; ++i)
|
||||
printf("%d ", b[i]);
|
||||
puts("");
|
||||
|
||||
free(b);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int icmp(const void *a, const void *b)
|
||||
{
|
||||
#define _I(x) *(int*)x
|
||||
return _I(a) < _I(b) ? -1 : _I(a) > _I(b);
|
||||
#undef _I
|
||||
}
|
||||
|
||||
/* filter items in place and return number of uniques. if a separate
|
||||
list is desired, duplicate it before calling this function */
|
||||
int uniq(int *a, int len)
|
||||
{
|
||||
int i, j;
|
||||
qsort(a, len, sizeof(int), icmp);
|
||||
for (i = j = 0; i < len; i++)
|
||||
if (a[i] != a[j]) a[++j] = a[i];
|
||||
return j + 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
|
||||
int i, len = uniq(x, sizeof(x) / sizeof(x[0]));
|
||||
for (i = 0; i < len; i++) printf("%d\n", x[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue