www.pudn.com > ezw_davis.rar > list.c


/* 
LIST.C 
 
Simple implementation of a linked list in ANSI-C. 
 
This file is part of my Embedded Zerotree Wavelet Encoder Tutorial. 
 
(C) C. Valens,  
 
Created    : 03/09/1999 
Last update: 29/09/1999 
*/ 
 
 
#include "list.h" 
#include  
#include  
 
 
typedef struct __list_element { 
  list_type data; 
  struct __list_element *next; 
} list_element; 
 
list_element *list_root = NULL; 
list_element *list_current = NULL; 
list_element *list_end = NULL; 
int list_length=0; 
 
void append_to_list(list_type d)//将新输入的节点放到链表的末尾 
{ 
  list_element *p; 
  p = malloc(sizeof(list_element)); 
  if (p!=NULL) { 
    p->data = d; 
    p->next = NULL; 
    if (list_root==NULL) list_root = p; 
    if (list_end!=NULL) list_end->next = p; 
    list_end = p; 
    list_length++; 
  } 
} 
 
void destroy_list(void)//清空整个链表 
{ 
  list_element *p; 
 
  p = list_root; 
  while (p!=NULL) { 
    list_root = p->next; 
    free(p); 
    p = list_root; 
  } 
  list_root = NULL; 
  list_current = NULL; 
  list_end = NULL; 
  list_length = 0; 
} 
 
void display_list(void)//输出链表中的所有节点 
{ 
  list_element *p; 
  p = list_root; 
  while (p!=NULL) { 
    printf("(%d,%d)",p->data.x,p->data.y); 
    p = p->next; 
  } 
} 
 
list_type get_list_element(int pos, char *found)//在链表中查找某一位置 
                                                //上的节点 
{ 
  list_element *p; 
  list_type d; 
  int i; 
 
  i = 0; 
  p = list_root; 
  while ((inext; 
  } 
  if ((idata; 
    *found = 1; 
  } 
  return d; 
} 
 
void put_list_element(list_type d, int pos, char *found)//将值d放入链表 
                                                        //指定的位置pos 
{ 
  list_element *p; 
  int i; 
 
  i = 0; 
  p = list_root; 
  while ((inext; 
  } 
  if ((idata = d; 
    *found = 1; 
  } 
} 
 
void reset_list(void)//将链表的当前指针放在头指针处 
{ 
  list_current = list_root; 
} 
 
list_type get_next_list_element(char *found)//得到链表当前位置的数据值 
                                            //将当前指针移向下一个元素  
{ 
  list_type d; 
 
  if (list_current==NULL) *found = 0; 
  else { 
    d = list_current->data; 
    *found = 1; 
    list_current = list_current->next; 
  } 
  return d; 
} 
 
void write_list_info(void) 
{ 
  printf("\n"); 
  printf("element size: %ld, length: %d\n",sizeof(list_type),list_length); 
} 
 
void initialize_list(void)//初始化链表 
{ 
  list_root = NULL; 
  list_current = NULL; 
  list_end = NULL; 
  list_length = 0; 
}