📄 list.c~
字号:
#include <stdio.h>#include <stdlib.h>#include "list.h"List * Create_List(void){ List *li; li = malloc(sizeof(List)); li->length = 0; li->head = NULL; li->tail = NULL; return li;}void Destroy_List(List *li,FreeData fre){ Dnode *temp=NULL; while(li->head) { temp = li->head; li->head = li->head->next; fre(temp->data); temp = NULL; } if (li) free(li); }void Print_List(List *li,PrintData p){ Dnode *temp = NULL; if(!li->head) return; else { temp = li->head; while(temp) { p(temp->data); temp = temp->next; } }}void Add_Node(List *li,DataType data){ Dnode *temp,*cur = NULL; temp = malloc(sizeof(Dnode)); temp->data = data; temp->next = NULL; temp->prev = NULL; if(!li->head) { li->head = temp; li->tail = temp; li->length ++; } else//add after tail { li->tail->next = temp; temp->prev = li->tail; li->tail = temp; li->length ++; }}void Insert_Node(List *li,InsertCond cond,DataType data){ Dnode *temp,*cur =NULL; temp = malloc(sizeof(Dnode)); temp->data = data; temp->prev = NULL; temp->next = NULL; if(!li->head)//list is null { li->head = temp; li->tail = temp; li->length++; } else if(1 == cond(li->head->data,data))//fisr node fit { temp->next = li->head->next; temp->prev = li->head; li->head->next = temp; li->length ++; } else { cur = li->head->next;//search from second node while(cur) { if(1 == cond(cur->data,data)) { temp->next = cur->next; temp->prev = cur; cur->next = temp; li->length ++; break; } cur = cur->next; } if(!cur)//no fit options,so add last { temp->prev = cur; cur->next = temp; li->tail = temp; li->length ++; } }}void Delete_Node(List *li,DelCond cond,FreeData fre){ Dnode *cur = NULL; Dnode *bak = NULL; cur = li->head; while(cur) { if(1 == cond(cur->data)) { if(NULL == cur->prev)//first node { li->head = cur->next; bak = cur; if(NULL == cur->next)//only one node { li->tail = NULL;//no nodes left } else//one more node { cur->next->prev = NULL;//second node as head } fre(bak->data); free(bak); bak = NULL; li->length --; } else { cur->prev->next = cur->next; if(!cur->next)//last node { li->tail = cur->prev; } else//middle node cur->next->prev = cur->prev; bak = cur; fre(bak->data); free(bak); bak = NULL; li->length --; } } cur = cur ->next; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -