[转载]c实现链表
________________________c_list.h__________________
#ifndef C_LIST_H
#define C_LIST_H
#ifndef ERR
#define ERR -1
#define OK 1
#endif
#ifndef MAX
#define MAX 100
#define MIN 0
#endif
typedef int status;
typedef int type;
typedef struct listitem {
type date;//节点数据
struct listitem *next;//指向下个节点
} list_node;//链表节点
typedef struct {
struct listitem *ptr;//链表头指针
int size;//链表长度
} list;//链表
list* list_init ( void );//初始化
status list_destroy( list* );//销毁
status add_node( list*, const type );//加入一个节点
status delete_all( list* );//清空
status delete_node( list*, list_node* );//删除一个节点
status insert_node( list*, const type );//插入一个节点
list_node* find_node( const list*, const type );//查找
status list_print( const list* );//打印
#endif
—————————c_list.c—————————————————
#include "stdio.h"
#include "stdlib.h"
#include "C_list.h"
list* list_init ( void )
{
list *p = ( list* )malloc( sizeof( list ) );
if( p == 0 )
return 0;
p->ptr = 0;
p->size = 0;
return p;
}
status list_destroy( list *pev )
{
if( pev == 0 )
return ERR;
delete_all( pev );
free( pev );
return OK;
}
status add_node( list *p, const type date )
{
list_node *pev =
( list_node* )malloc( sizeof( list_node ) );
if( pev == 0 )
return ERR;
pev->date = date;
pev->next = p->ptr;
p->ptr = pev;
p->size++;
return OK;
}
status delete_node( list *p, list_node *pev )
{
list_node *temp = pev;
if( pev == 0 )
return ERR;
pev = temp->next;
free( temp );
p->size--;
return OK;
}
status delete_all( list *pev )
{
int ix;
if( pev == 0 )
return ERR;
if( pev->size == 0 )
return ERR;
for( ix = 0; ix < pev->size; ++ix, ++pev->ptr )
delete_node( pev, pev->ptr );
return OK;
}
status insert_node( list *p, const type date )
{
list_node *pev = p->ptr; ;
if( p == 0 )
return ERR;
pev = find_node( p, date );
if( pev == 0 )
{
type ia;
printf( "输入要插入的数/n" );
scanf( "%d", &ia );
add_node( p, ia );
}
else
{
type ia;
list_node *pv =
( list_node* )malloc( sizeof( list_node ) );
if( pev == 0 )
return ERR;
printf( "输入要插入的数/n" );
scanf( "%d", &ia );
pv->date = ia;
pv->next = pev->next;
pev->next = pv;
p->size++;
}
return OK;
}
list_node* find_node( const list *pev , const type date )
{
int ix;
list_node *p = pev->ptr;
for( ix = 0; ix < pev->size; ++ix )
if( p->date == date )
return p;
else
p = p->next;
return 0;
}
status list_print( const list *pev )
{
int ix;
list_node *p = pev->ptr;
if( pev == 0 )
return ERR;
if( pev->size == 0 )
return OK;
for( ix = 0; ix < pev->size; ++ix )
{
printf( "%d/t", p->date );
p = p->next;
}
printf( "/n" );
return OK;
}
http://blog.csdn.net/owl2008/article/details/47678
----------------------------------------------------------------------
*
* **** LINKED LIST HEADER ****
*
* This is the header file for a generic linked list
*
*/
#ifndef LINKED_LIST_INC
#define LINKED_LIST_INC
typedef struct linkedlist { /* generic linked list */
struct linkedlist *nextlink; /* pointer to nextlink entry */
struct linkedlist *prevlink; /* pointer to previous entry */
void *data; /* pointer to data */
} LinkElement;
typedef struct linkeddef {
LinkElement *head; /* pointer to head of linked list */
LinkElement *tail; /* pointer to tail of linked list */
LinkElement *current; /* pointer to current entry */
} LinkList;
#define return_link_data(link) ((link)->data)
/* linklist.c */
LinkList *create_link_list(void);
void initialize_link_list(LinkList *list);
LinkElement *create_link_entry(void *data);
int add_link_to_head(LinkList *list, LinkElement *entry);
int add_link_to_tail(LinkList *list, LinkElement *entry);
void process_link_list(LinkList *list, void (*process_function)(LinkList *list, LinkElement *ent));
void free_link(LinkElement *lnk);
int remove_link(LinkList *list, LinkElement *lnk);
void *delete_link(LinkList *list, LinkElement *lnk);
LinkElement *find_link_entry(LinkList *list, void *key, int (*find_function)(LinkElement *lnk, void *key));
LinkElement *find_next_link_entry(LinkList *list, LinkElement *lnk, void *key, int (*find_function)(LinkElement *lnk, void *key));
int move_link_up(LinkList *list, LinkElement *lnk);
int move_link_down(LinkList *list, LinkElement *lnk);
int add_link_before_link(LinkList *list, LinkElement *new, LinkElement *exist);
int add_link_after_link(LinkList *list, LinkElement *new, LinkElement *exist);
int set_current_link(LinkList *list, LinkElement *lnk);
int set_head_current_link(LinkList *list);
int set_tail_current_link(LinkList *list);
int add_link_before_current(LinkList *list, LinkElement *lnk);
int add_link_after_current(LinkList *list, LinkElement *lnk);
int move_current_link_up(LinkList *list);
int move_current_link_down(LinkList *list);
int set_current_link_next(LinkList *list);
int cycle_current_link_next(LinkList *list);
int set_current_link_prev(LinkList *list);
int cycle_current_link_prev(LinkList *list);
int list_is_empty(LinkList *list);
int list_is_single_link(LinkList *list);
int link_is_current(LinkList *list, LinkElement *lnk);
int link_is_head(LinkList *list, LinkElement *lnk);
int link_is_tail(LinkList *list, LinkElement *lnk);
int current_is_head(LinkList *list);
int head_is_current(LinkList *list);
int current_is_tail(LinkList *list);
int tail_is_current(LinkList *list);
int add_new_current_link_before(LinkList *list, LinkElement *lnk);
int add_new_current_link_after(LinkList *list, LinkElement *lnk);
void *get_data_from_link(LinkElement *lnk);
void *get_data_from_current(LinkList *list);
void *get_data_from_head(LinkList *list);
void *get_data_from_tail(LinkList *list);
LinkElement *get_head_link(LinkList *list);
LinkElement *get_tail_link(LinkList *list);
LinkElement *get_current_link(LinkList *list);
LinkElement *get_next_link(LinkElement *lnk);
LinkElement *get_prev_link(LinkElement *lnk);
#endif
/* * **** LINKED LIST **** * * This file implements a generic linked list * */ #include <stdio.h> #include "linklist.h" #include "linklist.pro" LinkList * create_link_list(void) { LinkList *list; if ((list = (LinkList *)malloc(sizeof(LinkList))) != NULL) { /* list created - initialize it */ initialize_link_list(list); } return list; } void initialize_link_list(LinkList *list) { list->head = list->tail = list->current = NULL; } LinkElement * create_link_entry(void *data) { /* create a lnk entry pointing to data */ LinkElement *lnk; if ((lnk = (LinkElement *)malloc(sizeof(LinkElement))) != NULL) { /* got memory for lnk - fill it in */ lnk->nextlink = lnk->prevlink = NULL; /* not connected yet */ lnk->data = data; } return lnk; } int add_link_to_head(LinkList *list, LinkElement *entry) { /* add entry to head of linked list - zero is normal return */ if (entry == NULL || list == NULL) return 1; entry->nextlink = list->head; /* point to current head of list */ entry->prevlink = NULL; /* nothing in front of head */ if (list->head != NULL) list->head->prevlink = entry; /* bwd lnk */ list->head = entry; /* make new head of list */ if (list->tail == NULL) list->tail = entry; /* was empty - now tail */ return 0; } int add_link_to_tail(LinkList *list, LinkElement *entry) { /* add entry to tail of linked list - zero is normal return */ if (entry == NULL || list == NULL) return 1; entry->prevlink = list->tail; /* point to current head of list */ entry->nextlink = NULL; /* nothing in front of head */ if (list->tail != NULL) list->tail->nextlink = entry; /* fwd lnk */ list->tail = entry; /* make new tail of list */ if (list->head == NULL) list->head = entry; /* was empty - now head */ return 0; } void process_link_list(LinkList *list, void (*process_function)(LinkList *list, LinkElement *ent)) { /* loop thru list doing something */ LinkElement *lnk; for (lnk = list->head; lnk != NULL; lnk = lnk->nextlink) { (process_function)(list, lnk); } } void free_link(LinkElement *lnk) { free(lnk); } int remove_link(LinkList *list, LinkElement *lnk) { /* remove lnk from list */ if (list == NULL || lnk == NULL) return 1; /* bad input */ /* check for entry being the head and/or the tail */ if (list->head == lnk) list->head = lnk->nextlink; /* was the head */ if (list->tail == lnk) list->tail = lnk->prevlink; /* was the tail */ /* now remove lnk from the chain */ if (lnk->prevlink != NULL) lnk->prevlink->nextlink = lnk->nextlink; if (lnk->nextlink != NULL) lnk->nextlink->prevlink = lnk->prevlink; /* see if we are deleting the current entry */ if (list->current == lnk) list->current = NULL; /* no current */ return 0; } void * delete_link(LinkList *list, LinkElement *lnk) { /* remove lnk from list and delete it */ void *data; /* return value */ if (list == NULL || lnk == NULL) return NULL; /* bad input */ data = lnk->data; /* value to return */ /* now delete the lnk itself (but not the data) */ if (remove_link(list, lnk) == 0) free_link(lnk); return data; } LinkElement * find_link_entry(LinkList *list, void *key, int (*find_function)(LinkElement *lnk, void *key)) { /* find entry in lnk list matching key, using find_function */ LinkElement *lnk; /* loop thru linked list until match or end of list */ for (lnk = list->head; lnk != NULL && (find_function)(lnk, key); lnk = lnk->nextlink); return lnk; } LinkElement * find_next_link_entry(LinkList *list, LinkElement *lnk, void *key, int (*find_function)(LinkElement *lnk, void *key)) { /* find entry after lnk in lnk list matching key, using find_function */ /* loop thru linked list until match or end of list */ while (lnk != NULL && (find_function)(lnk, key)) lnk = lnk->nextlink; return lnk; } int move_link_up(LinkList *list, LinkElement *lnk) { /* move lnk towards top (or front) of list */ LinkElement *prevlink; /* entry to add in front of */ if (list == NULL || lnk == NULL) return 1; /* bad return */ if (lnk == list->head) return 0; /* already at head */ prevlink = lnk->prevlink; /* move before this entry */ if (prevlink == list->head) list->head = lnk; /* new head */ remove_link(list, lnk); /* delete lnk */ return add_link_before_link(list, lnk, prevlink); /* add in front of previous lnk */ } int move_link_down(LinkList *list, LinkElement *lnk) { /* move lnk towards bottom (or back) of list */ LinkElement *nextlink; /* entry to add after */ if (list == NULL || lnk == NULL) return 1; /* bad return */ if (lnk == list->tail) return 0; /* already at bottom */ nextlink = lnk->nextlink; /* move after this entry */ if (nextlink == list->tail) list->tail = lnk; /* new tail */ remove_link(list, lnk); /* delete lnk */ return add_link_after_link(list, lnk, nextlink); /* add after lnk */ } int add_link_before_link(LinkList *list, LinkElement *new, LinkElement *exist) { /* add new lnk before existing lnk */ if (list == NULL || new == NULL || exist == NULL) return 1; /* valid arguments - add before lnk, updating head if needed */ new->nextlink = exist; new->prevlink = exist->prevlink; exist->prevlink = new; if (new->prevlink == NULL) list->head = new; /* new head */ else new->prevlink->nextlink = new; return 0; } int add_link_after_link(LinkList *list, LinkElement *new, LinkElement *exist) { /* add new lnk after existing lnk */ if (list == NULL || new == NULL || exist == NULL) return 1; /* valid arguments - add after lnk, updating tail if needed */ new->prevlink = exist; new->nextlink = exist->nextlink; exist->nextlink = new; if (new->nextlink == NULL) list->tail = new; /* new tail */ else new->nextlink->prevlink = new; return 0; } int set_current_link(LinkList *list, LinkElement *lnk) { /* make given lnk the current entry in the list */ if (list == NULL) return 0; list->current = lnk; /* lnk can be NULL */ return 1; } int set_head_current_link(LinkList *list) { /* make the head the current entry */ if (list == NULL) return 0; /* bad list */ list->current = list->head; return 1; } int set_tail_current_link(LinkList *list) { /* make the head the current entry */ if (list == NULL) return 0; /* bad list */ list->current = list->tail; return 1; } int add_link_before_current(LinkList *list, LinkElement *lnk) { /* add this lnk before the current entry */ return add_link_before_link(list, lnk, list->current); } int add_link_after_current(LinkList *list, LinkElement *lnk) { /* add this lnk after the current entry in the list */ return add_link_after_link(list, lnk, list->current); } int move_current_link_up(LinkList *list) { /* move the current lnk up in list, leaving current pointer alone */ int ret; LinkElement *lnk; if (list == NULL) return 1; lnk = list->current; /* remember current */ ret = move_link_up(list, list->current); /* this deletes current */ list->current = lnk; /* reset current */ return ret; } int move_current_link_down(LinkList *list) { /* move the current lnk down in list, leaving current pointer alone */ int ret; LinkElement *lnk; if (list == NULL) return 1; lnk = list->current; /* remember current lnk */ ret = move_link_down(list, list->current); /* this will delete current */ list->current = lnk; /* restore current lnk */ return ret; } int set_current_link_next(LinkList *list) { /* move the current pointer to nextlink entry, leaving links alone */ if (list == NULL || list->current == NULL) return 1; if (list->current->nextlink != NULL) list->current = list->current->nextlink; return 0; } int cycle_current_link_next(LinkList *list) { if (list == NULL) return 1; /* bad arg */ if (list_is_empty(list)) return 2; /* fail on empty list */ if (list_is_single_link(list)) return 0; /* nothing to do */ /* if current lnk not set, make it the head */ if (list->current == NULL) return set_head_current_link(list); /* point to nextlink lnk. If at tail, then point to head */ if (current_is_tail(list)) return set_head_current_link(list); return set_current_link_next(list); } int set_current_link_prev(LinkList *list) { /* move the current pointer to nextlink entry, leaving links alone */ if (list == NULL || list->current == NULL) return 1; if (list->current->prevlink != NULL) list->current = list->current->prevlink; return 0; } int cycle_current_link_prev(LinkList *list) { if (list == NULL) return 1; /* bad arg */ if (list_is_empty(list)) return 0; /* fail on empty list */ if (list_is_single_link(list)) return 1; /* nothing to do */ /* if current lnk not set, make it the tail */ if (list->current == NULL) return set_tail_current_link(list); /* point to previous lnk; if at head, then point to tail */ if (current_is_head(list)) return set_tail_current_link(list); return set_current_link_prev(list); } int list_is_empty(LinkList *list) { if (list == NULL || list->head == NULL) return 1; /* nothing there */ return 0; } int list_is_single_link(LinkList *list) { if (list_is_empty(list)) return 0; if (list->head == list->tail) return 1; return 0; } int link_is_current(LinkList *list, LinkElement *lnk) { if (list == NULL) return 1; /* bad list */ return (lnk == list->current); } int link_is_head(LinkList *list, LinkElement *lnk) { if (list == NULL) return 1; /* bad list */ return (lnk == list->head); } int link_is_tail(LinkList *list, LinkElement *lnk) { if (list == NULL) return 1; /* bad list */ return (lnk == list->tail); } int current_is_head(LinkList *list) { if (list == NULL) return 0; /* bad list */ return (list->current == list->head); } int head_is_current(LinkList *list) { return current_is_head(list); } int current_is_tail(LinkList *list) { if (list == NULL) return 0; /* bad list */ return (list->current == list->tail); } int tail_is_current(LinkList *list) { return current_is_tail(list); } int add_new_current_link_before(LinkList *list, LinkElement *lnk) { /* add a new entry before the current & make it the new current */ int ret; if (list->current == NULL) { /* no current - add this to head and make it current */ if ((ret = add_link_to_head(list, lnk))) return ret; } else { /* current exists - just add it before */ if (add_link_before_current(list, lnk)) return 1; /* bad args */ } return set_current_link(list, lnk); } int add_new_current_link_after(LinkList *list, LinkElement *lnk) { /* add a new entry after the current & make it the new current */ int ret; if (list->current == NULL) { /* no current - add to tail and make it current */ if ((ret = add_link_to_tail(list, lnk))) return ret; } else { /* current exists - just add it after */ if (add_link_after_current(list, lnk)) return 1; /* bad args */ } return set_current_link(list, lnk); } void * get_data_from_link(LinkElement *lnk) { if (lnk == NULL) return NULL; return lnk->data; } void * get_data_from_current(LinkList *list) { if (list == NULL || list->current == NULL) return NULL; return list->current->data; } void * get_data_from_head(LinkList *list) { if (list == NULL || list->head == NULL) return NULL; return list->head->data; } void * get_data_from_tail(LinkList *list) { if (list == NULL || list->tail == NULL) return NULL; return list->tail->data; } LinkElement * get_head_link(LinkList *list) { if (list == NULL) return NULL; return list->head; } LinkElement * get_tail_link(LinkList *list) { if (list == NULL) return NULL; return list->tail; } LinkElement * get_current_link(LinkList *list) { if (list == NULL) return NULL; return list->current; } LinkElement * get_next_link(LinkElement *lnk) { if (lnk == NULL) return NULL; return lnk->nextlink; } LinkElement * get_prev_link(LinkElement *lnk) { if (lnk == NULL) return NULL; return lnk->prevlink; }
邮箱:steven9801@163.com
QQ: 48039387