Functions

icl_list.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include "icl_list.h"
Include dependency graph for icl_list.c:

Go to the source code of this file.

Functions

icl_list_t * icl_list_new ()
icl_list_t * icl_list_insert (icl_list_t *head, icl_list_t *pos, void *data)
int icl_list_delete (icl_list_t *head, icl_list_t *pos, void(*free_function)(void *))
icl_list_t * icl_list_search (icl_list_t *head, void *data, int(*compare)(void *, void *))
int icl_list_destroy (icl_list_t *head, void(*free_function)(void *))
int icl_list_size (icl_list_t *head)
icl_list_t * icl_list_first (icl_list_t *head)
icl_list_t * icl_list_next (icl_list_t *head, icl_list_t *pos)
icl_list_t * icl_list_prev (icl_list_t *head, icl_list_t *pos)
icl_list_t * icl_list_concat (icl_list_t *head1, icl_list_t *head2)
icl_list_t * icl_list_prepend (icl_list_t *head, void *data)
icl_list_t * icl_list_append (icl_list_t *head, void *data)

Detailed Description

Dependency free doubly linked list implementation.

Definition in file icl_list.c.


Function Documentation

icl_list_t* icl_list_append ( icl_list_t *  head,
void *  data 
)

Insert a node at the end of this list.

Parameters:
head -- the linked list
data -- the data to be inserted
Returns:
pointer to the new node. returns NULL on error.

Definition at line 286 of file icl_list.c.

{
  return(icl_list_insert(head, head->blink, data));
}

Here is the call graph for this function:

Here is the caller graph for this function:

icl_list_t* icl_list_concat ( icl_list_t *  head1,
icl_list_t *  head2 
)

Concatenate two linked lists.

Parameters:
head1 -- the first linked list
head2 -- the second linked list
Returns:
pointer to the new linked list, which consists of <head1,head2>. returns NULL on error.

Definition at line 247 of file icl_list.c.

{
  if(!head1 || !head2 || !head1->blink || !head2->flink) 
    return NULL;

  head1->blink->flink = head2->flink;
  head2->flink->blink = head1->blink;
  head1->blink = head2->blink;

  free(head2);

  return(head1);
}

int icl_list_delete ( icl_list_t *  head,
icl_list_t *  pos,
void(*)(void *)  free_function 
)

Delete the specified node.

Parameters:
head -- the linked list containing the node to be deleted
pos -- the node to be deleted
free_function -- pointer to function that frees the node data
Returns:
0 on success, -1 on failure.

Definition at line 84 of file icl_list.c.

{
  if (!pos || !head) return -1;
  if (pos == head) return -1;

  if(free_function && pos->data) 
    (*free_function)(pos->data);

  pos->blink->flink = pos->flink;

  if(pos->flink) 
    pos->flink->blink = pos->blink; 
  else 
    head->blink = pos->blink;   /* pos at end of list */

  free(pos);

  return 0;
}

Here is the caller graph for this function:

int icl_list_destroy ( icl_list_t *  head,
void(*)(void *)  free_function 
)

Frees the resources associated with this linked list.

Parameters:
head -- the linked list to be destroyed
free_function -- pointer to function that frees the node's data
Returns:
0 on success, -1 on failure.

Definition at line 143 of file icl_list.c.

{
  icl_list_t *node, *tmpnode;

  if (!head) return -1;

  for(node=head; node!=NULL; ) {
    tmpnode = node->flink;

    if(free_function!=NULL && node->data!=NULL) 
      (*free_function)(node->data);

    free(node);
    node = tmpnode;
  }

  return 0;
}

Here is the caller graph for this function:

icl_list_t* icl_list_first ( icl_list_t *  head  ) 

Get the first item in this linked list.

Parameters:
head -- the linked list
Returns:
pointer to the first item. returns NULL on error.

Definition at line 192 of file icl_list.c.

{
  if(head) 
    return head->flink;

  return NULL;
}

Here is the caller graph for this function:

icl_list_t* icl_list_insert ( icl_list_t *  head,
icl_list_t *  pos,
void *  data 
)

Insert a new node after the specified node.

Parameters:
head -- the linked list
pos -- points to the position of the new node (it will be inserted after this node)
data -- pointer to the data that is to be inserted
Returns:
pointer to the new node. returns NULL on error.

Definition at line 49 of file icl_list.c.

{
  icl_list_t *node;

  if(!head || !pos) return NULL;

  node = (icl_list_t*)malloc(sizeof(icl_list_t));

  if(!node) return NULL;

  node->blink = pos;
  node->flink = pos->flink;
  node->data = data;

  if(pos->flink) 
    pos->flink->blink = node;
  else 
    head->blink = node; /* node at end of list */

  pos->flink = node;

  return node;
}

Here is the caller graph for this function:

icl_list_t* icl_list_new (  ) 

Create new linked list.

Returns:
pointer to new linked list. returns NULL on error.

Definition at line 22 of file icl_list.c.

{
  icl_list_t *node;

  node = (icl_list_t*)malloc(sizeof(icl_list_t));

  if(!node) return NULL;

  node->flink = NULL;
  node->blink = node;
  node->data = NULL;

  return(node);
}

Here is the caller graph for this function:

icl_list_t* icl_list_next ( icl_list_t *  head,
icl_list_t *  pos 
)

Get the node following the specified node.

Parameters:
head -- the list containing the specified node
pos -- the node whose successor should be returned
Returns:
pointer to the next node. returns NULL on error.

Definition at line 210 of file icl_list.c.

{
  if(pos) 
    return pos->flink;

  return NULL;
}

Here is the caller graph for this function:

icl_list_t* icl_list_prepend ( icl_list_t *  head,
void *  data 
)

Insert a node at the beginning of this list.

Parameters:
head -- the linked list
data -- the data to be inserted
Returns:
pointer to the new node. returns NULL on error.

Definition at line 271 of file icl_list.c.

{
  return(icl_list_insert(head, head, data));
}

Here is the call graph for this function:

Here is the caller graph for this function:

icl_list_t* icl_list_prev ( icl_list_t *  head,
icl_list_t *  pos 
)

Get the node preceding the specified node.

Parameters:
head -- the list containing the specified node
pos -- the node whose predecessor should be returned
Returns:
pointer to the previous node. returns NULL on error.

Definition at line 228 of file icl_list.c.

{
  if(pos && pos->blink!=NULL) 
    return pos->blink;

  return NULL;
}

icl_list_t* icl_list_search ( icl_list_t *  head,
void *  data,
int(*)(void *, void *)  compare 
)

Finds a data item in the specified linked list.

Parameters:
head -- the linked list
data -- the data to be found
compare -- function that compares the data items
Returns:
pointer to the node, if found. otherwise returns NULL.

Definition at line 115 of file icl_list.c.

{
  icl_list_t *node;

  if (!head) return NULL;

  for (node=head->flink; node!=NULL; node=node->flink) {
    if(!node->data) 
      continue;
    if((compare && (*compare)(node->data, data)==0)) 
      break;
    else if (node->data==data) 
      break; /* compare == NULL, then direct comparison of pointers */
  }

  return(node);
}

Here is the caller graph for this function:

int icl_list_size ( icl_list_t *  head  ) 

Get the number of items in this linked list.

Parameters:
head -- the linked list
Returns:
the number of items in the list. returns -1 on error.

Definition at line 171 of file icl_list.c.

{
  int size=0;

  if(!head) return -1;

  while((head=head->flink)) 
    size++;

  return size;
}

Here is the caller graph for this function: