- Properties:
- Efficient addition and removal from any position
- Linear traversal to access arbitrary elements
- Efficient dynamic expansion
- List representation:
struct host_list {
struct host_list *next;
struct in_addr addr;
} *hosts;
- Traversal:
int
search_host(addr)
struct in_addr addr;
{
struct host_list *hp;
if (!hosts)
return(0);
for (hp = hosts; hp != NULL; hp = hp->next) {
if (hp->addr.s_addr == addr.s_addr)
return(1);
}
return(0);
}
- Element addition:
void
remember_host(addr)
struct in_addr addr;
{
struct host_list *hp;
if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
err(1, "malloc");
/* NOTREACHED */
}
hp->addr.s_addr = addr.s_addr;
hp->next = hosts;
hosts = hp;
}