main.c
#include "linklist.h"
#include <stdio.h>
#include <stdlib.h>
int a(datatype a, datatype b)
{
return *(int *)a < *(int *)b;
}
void b(datatype data){
printf("%d ", *(int *)data);
}
int main(){
int arr[] = {3, 4, 66, 76, 334, 12, 32};
list *l;
l = list_create();
for (unsigned long i = 0;i < sizeof(arr)/sizeof(*arr);i++){
list_order_insert(l, arr+i,a);
}
list_insert_at(l,3,arr+6);
list_show(l, b);
int *num = NULL;
list_delete_at(l, 2, &num);
printf("%d删除\n", *num);
list_show(l, b);
list_destory(l);
exit(0);
}
linklist.h
#ifndef LINKLIST_H_
#define LINKLIST_H_
typedef void *datatype;
typedef struct node_st{
datatype data;
struct node_st *next;
}list;
list* list_create();
void list_destory(list *me);
int list_insert_at(list*, int, datatype);
int list_order_insert(list* me, datatype data, int (*)(datatype,datatype));
int list_delete_at(list*, int, datatype);
int list_delete(list* me,datatype data, int (*)(datatype, datatype));
int list_isempty(list*);
void list_show(list *me, void (*)(datatype));
#endif // LINKLIST_H_
linklist.c
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "linklist.h"
list* list_create()
{
list *head;
head = malloc(sizeof(list));
if(head == NULL)
{
return NULL;
}
return head;
}
void list_destory(list* me)
{
if(list_isempty(me))
{
return;
}
list *node = me->next, *next;
//需要创建一个结点保存传入的me,和另一个结点保存下一个结点便于free
while(node != NULL)
{
next = node->next;
free(node);
node = next;
}
/*for(node = me->next; next != NULL; node = next)
{
next = node->next;
free(node);
}*/
free(me);
}
/*
** 在i位置插入元素data
*/
int list_insert_at(list* me, int i, datatype data)
{
int j = 0;
list *node = me->next, *newnode;
//一个结点用来复制me,一个用来保存新结点
if(i < 0)
{
return -1;
}
while(j < i && node != NULL)//移动到i-1
{
node = node->next;
j++;
}
if(node)
{
newnode = malloc(sizeof(list));
if(newnode == NULL)
{
return -2;
}
newnode->data = data;
newnode->next = node->next;//先确定后面的结点,再看前面的结点
node->next = newnode;
return 0;
}
else
{
return -3;
}
}
// 排序插入;传入比较大小的函数
int list_order_insert(list* me, datatype data, int (*f)(datatype,datatype))
{
if(me == NULL)
{
return EINVAL;
}
list *p = me,*q;
while(p->next && f(p->next->data, data))
{
p = p->next;
}
q = malloc(sizeof(list));
if(q == NULL)
{
return -1;
}
q->data = data;
q->next = p->next;
p->next = q;
return 0;
}
/*指定的位置进行删除
*/
int list_delete_at(list* me, int i, datatype data)
{
if(me == NULL)
{
return EINVAL;
}
list *node = me->next;
for(int j = 1; j < i; j++)//移动node到i前一位
{
if(node->next == NULL)
{
return -1;
}
node = node->next;
}
list *tempnode;
tempnode = node->next;//先保存后继
node->next = tempnode->next;
*(void **)data = tempnode->data;
free(tempnode);
return 0;
}
//删除链表中某个元素
int list_delete(list* me,datatype data, int f(datatype, datatype))
{
list *p = me->next;
while(p->next && f(p->next->data, data))
{
p = p->next;//遍历到指定位置的前一个
}
if(p->next == NULL)
{
return -1;
}
else
{
list *q;
q = p->next;//先保存后继,再free
p->next = q->next;
free(q);
return 0;
}
return -2;
}
int list_isempty(list* me)
{
if(me == NULL)
{
return 1;
}
return 0;
}
void list_show(list *me, void f(datatype))
{
list *node = me->next;
if (list_isempty(me))
{
return;
}
while(node != NULL)
{
f(node->data);
node = node->next;
}
printf("\n");
}