#include <stdio.h>
#include <stdlib.h>
#include "list.h"
List * Create_List(void)
{
List *temp;
temp = malloc(sizeof(List));
temp->length = 0;
temp->head = NULL;
return temp;
}
void Destroy_List(List *li,DelData FreeData)
{
Node *temp = NULL,*bak=NULL;
temp = li->head;
while(temp)
{
bak = temp;
FreeData(bak->data);
temp = temp->next;
}
if(li)
free(li);
}
void Add_Node(List *li,DataType data)
{
Node *new,*temp=NULL;
new = malloc(sizeof(Node));
new->data = data;
new->next = NULL;
if(!li->head)
{
li->head = new;
}
else
{
temp = li->head;
while(temp->next)
{
temp = temp->next;
}
temp->next = new;
}
li->length ++;
}
void Print_List(List *li,PrintData p)
{
Node *temp;
temp = li->head;
if(!temp)
return;
while(1)
{
p(temp->data);
temp = temp->next;
if (NULL == temp)
break;
}
}
void Insert_Node(List *li,InsertCond cond,DataType data)
{
Node *new,*temp;
new = malloc(sizeof(Node));
new->data = data;
new->next = NULL;
if(!li->head)//if list is null
{
li->head = new;//insert new
li->length ++;
}
if(1 == cond(li->head->data,data))//if head node fit
{
new->next = li->head;
li->head = new;
}
else
{
temp = li->head;
while(temp->next)
{
if(1 == cond(temp->next->data,data))
{
new->next = temp->next;
temp->next = new;
li->length++;
}
temp = temp->next;
}
temp->next = new;//if find nothing, add last
li->length ++;
return;
}
}
void Delete_Node(List *li,DelCond cond,DelData del)
{
Node *temp = NULL,*bak = NULL;
temp = li->head;
bak = li->head;
while(temp && temp->next)//the node is not first node
{
if(1 == cond(temp->next->data))//include last node,compare next node 's data;
{
bak = temp->next;//backup the pointer of node want to del;
temp->next = temp->next->next;//change pointer before del node
del(bak->data);//free data if data include pointer
free(bak);//free node
li->length --;
}
else
{
temp = temp->next;
}
}
if(1 == cond(li->head->data))//the node is first node
{
temp = li->head;
li->head = temp->next;
del(temp->data);
free (temp);
li->length --;
}
}
List * Invert_List(List *li)
{
Node * pre = NULL,*cur=NULL,*nex=NULL;
if(NULL == li->head || NULL == li->head->next)
return li;
else
{
pre = li->head;
cur = pre->next;
pre->next = NULL;
while(nex = cur->next)
{
cur->next = pre;
pre = cur;
cur = nex;
}
li->head = cur;
cur->next = pre;
}
return li;
}
list_si.rar_单向链表
版权申诉
64 浏览量
2022-09-20
14:45:40
上传
评论
收藏 5KB RAR 举报
我虽横行却不霸道
- 粉丝: 75
- 资源: 1万+
最新资源
- 演讲稿.txt
- 基于Python的爬虫案例-软科中国大学TOP200
- 碳排放权交易明细数据(2024年5月更新).xlsx
- 特殊文件属性命令chattr和lsattr
- HTML、CSS 和 JavaScript动态、交互式的网页 .txt
- b0cd8f9b23d4e5e381b6a8fd8ee0e907.JPG
- ff45d61c5900e45634cf4cac6cff61a1.JPG
- springboot.springboot.springboot.springboot.txt
- linux-进程与服务管理
- 毕业设计基于Django+MySQL+Redis实现简单的天气预报系统python源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈