git.fiddlerwoaroof.com
Raw Blame History
#include <stdio.h>
#include <stdlib.h>

typedef unsigned int item_type;
typedef struct list {
  item_type item;
  struct list * next;
} list;

list * search_list(list *l, item_type x) {
  if (l == NULL) return NULL;

  if (l->item == x) {
    return l;
  } else {
    return search_list(l->next, x);
  }
}

void insert_list(list **l, item_type x) {
  list *p;

  p = malloc(sizeof(list) );
  p->item = x;
  p->next = *l;
  *l = p;
}

list *predecessor_list(list *l, item_type x) {
  if ((l == NULL) || (l->next == NULL)) {
    printf("Error: predecessor sought on null list.\n");
    return NULL;
  }

  if ((l->next)->item == x) {
    return l;
  } else {
    return predecessor_list(l->next, x);
  }
}

delete_list(list **l, item_type x) {
  list *p;
  list * pred;

  p = search_list(*l, x);
  if (p != NULL) {
    pred = predecessor_list(*l, x);
    if (pred == NULL) {
      *l = p->next;
    } else {
      pred->next = p->next;
    }

    free(p);
  }
}

item_type i(list *l) {
  if (l == NULL) return -1;
  return l->item;
}
int main() {
  list *l = NULL;
  insert_list(&l, 123);
  insert_list(&l, 234);
  delete_list(&l, 123);
  printf("%u\n", i(search_list(l, 123)));
  return 42;
}