* link.h
*
* Created on: Nov 7, 2010
* Author: jenson
*/
#ifndef LINK_H_
#define LINK_H_
typedef int item_type;
typedef struct _link_ * link;
struct _link_{
item_type item;
link next;
};
extern int l_count(link x);
extern void l_traverse(link l,void (*visit)(link));
extern void l_traverse_r(link l,void (*visit)(link));
extern link l_delete(link l,item_type e);
#endif /* LINK_H_ */
/*
* link.c
*
* Created on: Nov 7, 2010
* Author: jenson
*/
#include "link.h"
#include <stdio.h>
int count(link x) {
if (x == NULL) {
return 0;
}
return 1 + count(x->next);
}
void l_traverse(link l, void(*visit)(link)) {
if (l == NULL)
return;
(*visit)(l);
l_traverse(l->next, visit);
}
void l_traverse_r(link l, void(*visit)(link)) {
if (l == NULL) {
return;
}
l_traverse_r(l->next, visit);
(*visit)(l);
}
link l_delete(link l, item_type e) {
if (l == NULL) {
return NULL;
}
if (l->item == e) {
link t = x->next;
free(x);
return t;
}
x->next = l_delete(x->next, e);
return x;
}