http://www.oocities.org/SiliconValley/Peaks/8778/TAU_advprog.html
Linked Linked of Integer Numbers
Here you can find an example of implementation of linked list. It is
implemented for integer numbers. The implementation consists of
a header file (int_list.h) and a source
file (int_list.c). All functions are
implemeted in 2 ways: iterative and recursive. Also an example of
the list use is provided in a separate file
(main.c).
Also it is possible to download all these files in zip-archive:
just click here: int_list.zip and choose
"Save it to disk".
int_list.h
/* int_list.h - header for linked list of integers */
#ifndef __INTLIST_H__
#define __INTLIST_H__
/* a cell (node) of a list */
typedef struct int_list_node
{
int val; /* list element */
struct int_list_node* next; /* ptr to next cell */
} NODE;
/* list may/may not be: unique/ordered */
enum list_type
{
AS_IS = 0x0,
UNIQUE = 0x1,
ORDERED = 0x2,
UNIQUE_ORDERED = 0x3 /* must be UNIQUE | ORDERED */
};
/* operations on list */
/* insertion to list */
void il_insert( NODE** list, int x, enum list_type type);
/* check if list contains an integer */
int il_lookup( NODE* list, int x);
/* deletion from list all cells with value x */
void il_remove( NODE** list, int x);
/* printing of list to stdout */
void il_print( NODE* list);
#endif
int_list.c
/* int_list.c - implementation of operations on list of integers */
#include <stdio.h>
#include <malloc.h>
#include <int_list.h>
#ifndef RECURSIVE_FUNCS
/* Iterative implementation of operations on list of integers */
void il_insert( NODE** list, int x, enum list_type type)
{
NODE** node;
NODE** new_node;
NODE* next;
/* if our list is empty - that's all */
if( *list == NULL)
new_node = list;
else
{
/* let's find the last element of the list */
for( node = list; (*node) != NULL;
node = &( (*node)->next))
{
if( type & UNIQUE)
{
if( (*node)->val == x)
return; /* x already exists there */
}
if( type & ORDERED)
{
if( (*node)->val >= x)
break;
}
/* else nothing */
}
new_node = node;
}
next = *new_node;
*new_node = malloc( sizeof( NODE));
(*new_node)->val = x;
(*new_node)->next = next;
}
int il_lookup( NODE* list, int x)
{
NODE* node;
for( node = list; node != NULL; node = node->next)
if( node->val == x)
return( 1);
return( 0);
}
void il_remove( NODE** list, int x)
{
NODE** node;
NODE* next;
for( node = list; *node != NULL; )
{
if( (*node)->val == x)
{
next = (*node)->next;
free( *node); /* if the nodes were malloc'ed */
*node = next;
}
else
node = &( (*node)->next);
}
}
void il_print( NODE* list)
{
NODE* node;
for( node = list; node != NULL; node = node->next)
printf( "%d ", node->val);
printf( "\n");
}
#else
/* Recursive implementation of operations on list of integers */
void il_insert( NODE** list, int x, enum list_type type)
{
NODE* next;
if( *list != NULL && type & UNIQUE)
{
if( (*list)->val == x)
return; /* x already exists there */
}
if( *list == NULL ||
( type & ORDERED && (*list)->val >= x))
{
next = *list;
*list = malloc( sizeof( NODE));
(*list)->val = x;
(*list)->next = next;
return;
}
il_insert( &( (*list)->next), x, type);
}
int il_lookup( NODE* list, int x)
{
if( list == NULL)
return( 0);
else if( list->val == x)
return( 1);
else
return il_lookup( list->next, x);
}
void il_remove( NODE** list, int x)
{
NODE* next;
if( *list == NULL)
return;
if( (*list)->val == x)
{
next = (*list)->next;
free( *list); /* if the nodes were malloc'ed */
*list = next;
il_remove( list, x);
}
else
il_remove( &( (*list)->next), x);
}
void il_print( NODE* list)
{
if( list == NULL)
{
printf( "\n");
return;
}
printf( "%d ", list->val);
il_print( list->next);
}
#endif
main.c
/* main.c - test of operations on lists of integers */
#include <stdio.h>
#include <int_list.h>
static void check_lookup( NODE* list, int val)
{
printf( "lookup for [%d] returned [%d]\n",
val, il_lookup( list, val));
}
static void check_remove( NODE** list, int val)
{
printf( "removing [%d]... => ", val);
il_remove( list, val);
il_print( *list);
}
static void check_list( int* vals, enum list_type type)
{
int i;
NODE* list = 0;
printf( "================\n");
for( i = 0; vals[i] != -100; i ++) /* -100 - end of values */
il_insert( &list, vals[i], type);
printf( "After insertion => ");
il_print( list);
check_lookup( list, 1);
check_lookup( list, 2);
check_lookup( list, 3);
check_lookup( list, 4);
check_lookup( list, 5);
check_lookup( list, 6);
check_lookup( list, 7);
check_remove( &list, 1);
check_remove( &list, 2);
check_remove( &list, 3);
check_remove( &list, 4);
check_remove( &list, 5);
check_remove( &list, 6);
check_remove( &list, 7);
}
int main()
{
/* -100 - sign of the end of values */
static int init[] = { 1, 2, 5, 6, 3, 2, 6, 3, 1, 3, -100 };
check_list( init, AS_IS);
check_list( init, UNIQUE);
check_list( init, ORDERED);
check_list( init, UNIQUE_ORDERED);
return( 0);
}