Looks like a little non-C (or C++) syntax there.
Maybe you mean something along these lines.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Segment
{
int n;
struct Segment *next;
} Segment;
Segment *new_segment(int n, Segment *next)
{
Segment *seg = malloc(sizeof *seg);
if (!seg) { perror("new_segment"); exit(EXIT_FAILURE); }
seg->next = next;
seg->n = n;
return seg;
}
Segment *prepend(Segment *root, int n)
{
return new_segment(n, root);
}
Segment *append(Segment *root, int n)
{
Segment *seg = new_segment(n, NULL);
if (!root) return seg;
Segment *s = root;
while (s->next) s = s->next;
s->next = seg;
return root;
}
void print(const Segment *segs)
{
for ( ; segs; segs = segs->next) printf("%d ", segs->n);
printf("\n");
}
int cmp_less (const Segment *a, const void *b) { return a->n < *(int*)b; }
int cmp_greater(const Segment *a, const void *b) { return a->n > *(int*)b; }
Segment *elem_forward_until(
Segment* segs,
int(* cmp)(const Segment*, const void*),
const void* stop)
{
while (segs && cmp(segs, stop)) segs = segs->next;
return segs;
}
int main()
{
const int init[] = {4, 5, 3, 6, 2, 7, 1, 9, -1}; // -1 marks end of init list
Segment *segs = NULL;
for (const int *n = init; *n != -1; ++n) segs = append(segs, *n);
print(segs);
Segment *s = elem_forward_until(segs, cmp_less, &(int){7});
if (s) printf("%d\n", s->n); else printf("nil\n");
s = elem_forward_until(segs, cmp_greater, &(int){1});
if (s) printf("%d\n", s->n); else printf("nil\n");
return 0;
}