The list.h header file, which provides a simple linked list type
/*
* Generic linked list node structure--can hold either
* a character string or another list as data.
*/
typedef struct listnode_s {
struct listnode_s *next;
union {
void *data;
struct list_s *list;
const char *str;
} u;
} listnode_t;
* Generic linked list node structure--can hold either
* a character string or another list as data.
*/
typedef struct listnode_s {
struct listnode_s *next;
union {
void *data;
struct list_s *list;
const char *str;
} u;
} listnode_t;
typedef struct list_s {
listnode_t *head;
listnode_t *tail;
} list_t;
extern void appendNode(list_t *, listnode_t *);
extern listnode_t *removeHead(list_t *);
extern void concatList(list_t *, list_t *);
extern list_t *copyOf(list_t);
extern listnode_t *newNode(void *);
extern list_t *newList();
listnode_t *head;
listnode_t *tail;
} list_t;
extern void appendNode(list_t *, listnode_t *);
extern listnode_t *removeHead(list_t *);
extern void concatList(list_t *, list_t *);
extern list_t *copyOf(list_t);
extern listnode_t *newNode(void *);
extern list_t *newList();
The list.c source file, which provides a simple linked list type.
#include <malloc.h>
#include “list.h”
/* Appends a listnode_t to a list_t. */
void appendNode(list_t *list, listnode_t *node)
{
node->next = NULL;
if (list->head)
{
list->tail->next = node;
list->tail = node;
}
else
list->head = list->tail = node;
}
/* Removes the first node from a list_t and returns it. */
listnode_t *removeHead(list_t *list)
{
listnode_t *node = 0;
if (list->head)
{
node = list->head;
list->head = list->head->next;
if (list->head == NULL)
list->tail = NULL;
node->next = NULL;
}
return node;
}
/* Concatenates two lists into the first list. */
void concatList(list_t *first, list_t *second)
{
if (first->head)
{
if (second->head)
{
first->tail->next = second->head;
first->tail = second->tail;
}
}
else
*first = *second;
second->head = second->tail = NULL;
}
/* Returns a copy of a list_t from the heap. */
list_t *copyOf(list_t list)
{
list_t *new = (list_t *) malloc(sizeof(list_t));
*new = list;
return new;
}
/* Allocates a new listnode_t from the heap. */
listnode_t *newNode(void *data)
{
listnode_t *new = (listnode_t *)
malloc(sizeof(listnode_t));
new->next = NULL;
new->u.data = data;
return new;
}
/* Allocates an empty list_t from the heap. */
list_t *newList()
{
list_t *new = (list_t *) malloc(sizeof(list_t));
new->head = new->tail = NULL;
return new;
}
*first = *second;
second->head = second->tail = NULL;
}
/* Returns a copy of a list_t from the heap. */
list_t *copyOf(list_t list)
{
list_t *new = (list_t *) malloc(sizeof(list_t));
*new = list;
return new;
}
/* Allocates a new listnode_t from the heap. */
listnode_t *newNode(void *data)
{
listnode_t *new = (listnode_t *)
malloc(sizeof(listnode_t));
new->next = NULL;
new->u.data = data;
return new;
}
/* Allocates an empty list_t from the heap. */
list_t *newList()
{
list_t *new = (list_t *) malloc(sizeof(list_t));
new->head = new->tail = NULL;
return new;
}
The hash.h header file, which provides a simple character string hash function.
unsigned int hash(const char *);
Listing III.9g. The hash.c source file, which provides a simple character string hash function.
#include “hash.h”
/* This is a simple string hash function */
unsigned int hash(const char *string)
{
unsigned h = 0;
while (*string)
h = 17 * h + *string++;
return h;
}
No comments:
Post a Comment