最近写了链表的实现。先贴代码。
(代码是在linux gcc下编译,有可能和vs上编译有差别)
1,双向链表的实现:
/**
*@breif带头结点的双向链表的实现
*@authorLinf *@data2012-12-28 10:45
*@detail实现双向链表的一系列操作。init,insert,delete,print...etc
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _doublelist{
int num;
struct _doublelist *next;
struct _doublelist *down;
}doublelist;
/**
*func:初始化双向链表
*param:NULL
*return:struct doublelist's point
*/
static doublelist* initDoubleList()
{
doublelist* list = NULL;
list = (doublelist *)malloc(sizeof(doublelist));
if(list == NULL)
return NULL;
list->next = NULL;
list->down = NULL;
return list;
}
/**
*func:计算链表的长度
*param:head - 头结点
*return:NULL
*/
static int numDoubleList(doublelist* head)
{
int len = 0;
while(head->next)
{
len++;
head = head->next;
}
return len;
}
/**
*func:打印双向链表
*param:list - 双向链表的头结点
*return:NULL
*/
static void printDoubleList(doublelist* list)
{
doublelist* head = list;
printf("------------next------------\n");
while(head->next)
{
printf("data:%d\n", head->next->num);
head = head->next;
}
printf("------------next------------\n");
head = list;
printf("------------down------------\n");
while(head->down)
{
printf("data:%d\n", head->down->num);
head = head->down;
}
printf("------------down------------\n");
return ;
}
/**
*func:释放资源,very important。防止内存泄露
平时编程也要注意。慢慢养成良好的编程习惯。慢慢的就习以为常了。哈哈
*param:list - 头结点
*return:NULL
*/
static void freeDoubleList(doublelist** list)
{
doublelist* head = NULL;
doublelist* now = NULL;
head = *list;
while(head -> next)
{
now = head->next;
head = head->next;
if(now)
{
free((void *)now);
now = NULL;
}
}
printf("free doublelist ok\n");
return ;
}
/**
*func:通过num值的大小顺序插入双向链表节点。(num类型可扩展)
*param:list -- 头结点指针的指针,num -- 插入的num值
*return: 0 success insert. -1 failed insert
*/
static int insertDoubleList(doublelist** list , int num)
{
doublelist* head = *list;
doublelist* downhead = *list;
if(head == NULL)
return -1;
doublelist* point = NULL;
point = initDoubleList();
if(point == NULL)
return -1;
point->num = num;
//双向链表中还没有节点,
if(head ->next == NULL || head->down == NULL)
{
head->next = point;
head->down = point;
return 0;
}
while(head->next && head->next->num <= num)
head = head->next;
//插入到最后
if(head->next == NULL)
{
head->next = point;
point->down = head;
downhead->down = point;
}
//插入到中间
else
{
point->next = head->next;
point->down = head;
head->next->down = point;
head->next = point;
}
return 0;
}
/**
*func:通过location删除相应的节点
(其实这个接口的实用价值不大。只是方便操作。just it)
注意删除的节点要free。否则内存泄露
*param:list -- 双向链表指针的指针。(实际传的是存储双向链表指针的地址 ), location -- 删除的位置
*return: 0 success delete. -1 failed delete
*/
static int deleteDoubleList(doublelist** list, int location)
{
doublelist* head = *list;
doublelist* now = NULL;
doublelist* last = NULL;
if(head == NULL)
return -1;
//先判断location的合法性
if(location <= 0 || location > numDoubleList(head))
return -1;
//删除第一个特殊判断处理
if(location == 1)
{
now = head->next;
head->next = now->next;
head->next->down = NULL;
if(now)
{
free((void *)now);
now = NULL;
}
return 0;
}
int num = 0;
while(head->next && num++ < location)
head = head->next;
//删除最后一个节点
if(head->next == NULL)
{
now = *list;
head->down->next = NULL;
now->down = head->down;
if(head)
{
free((void *)head);
head = NULL;
}
}
//删除中间的节点
else
{
printf("line=%d\n", __LINE__);
now = head->next;
last = head->down;
now->down = head->down;
last->next = head->next;
if(head)
{
free((void *)head);
head = NULL;
}
}
return 0;
}
int main(int argc, char** argv)
{
doublelist* head = initDoubleList();
if(head == NULL)
return -1;
insertDoubleList(&head, 3);
insertDoubleList(&head, 5);
insertDoubleList(&head, 4);
printDoubleList(head);
deleteDoubleList(&head, 1);
printDoubleList(head);
freeDoubleList(&head);
return 0;
}
网友评论