Skip to content

Instantly share code, notes, and snippets.

@Abreto
Created October 5, 2012 09:41
Show Gist options
  • Save Abreto/3838968 to your computer and use it in GitHub Desktop.
Save Abreto/3838968 to your computer and use it in GitHub Desktop.
数据结构(用C语言描述)
/**
* 单链表之实现
*
* 星枵<m@abreto.net
* 20121005
**/
/** 双链表结点结构 */
typedef struct _listnode
{
int data;
struct _listnode *prev;
struct _listnode *next;
}listnode, *p_listnode;
/** 双链表结构 */
typedef struct
{
p_listnode head;
}list, *p_list;
/**
* 在双链表L中搜索k
*
* @参数: list 操作双链表, int 查找的键.
* @返回: 搜索到的元素的指针, 否则返回NULL.
**/
p_listnode list_search(list L, int k)
{
p_listnode x = L.head;
while( (x != NULL) && (x->data != k) )
x = x->next;
return x;
}
/**
* 将x插入到双链表L的前端
*
* @参数: p_list 指向操作双链表的指针, p_listnode 指向x的指针
**/
void list_insert(p_list L, p_listnode x)
{
x->next = L->head;
if( L->head != NULL )
L->head->prev = x;
L->head = x;
x->prev = NULL;
}
/**
* 从链表L中删除一个元素x
*
* @参数: p_list 指向操作双链表的指针, p_listnode 指向x的指针
**/
void list_delete(p_list L, p_listnode x)
{
if( x->prev != NULL )
x->prev->next = x->next;
else
L->head = x->next;
if(x->next != NULL)
x->next->prev = x->prev;
// free(x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment