LinkList.h源码
#ifndef LINKLIST_H
#define LINKLIST_H
#include<stdlib.h>
#include<stdio.h>
typedef struct LINKNODE{
struct LINKNODE* next;
}LinkNode;
typedef struct LINKLIST{
LinkNode head;
int size;
}LinkList;
typedef void(*PRINTNODE)(LinkNode*);
typedef int(*COMPARENODE)(LinkNode*, LinkNode*);
LinkList* Init_LinkList();
void Insert_LinkList(LinkList* list, int pos, LinkNode* data);
void Remove_LinkList(LinkList* list, int pos);
int Find_LinkList(LinkList* list, LinkNode* data, COMPARENODE compare);
int Size_LinkList(LinkList* list);
void Print_LinkList(LinkList* list, PRINTNODE print);
void FreeSpace_LinkList(LinkList* list);
#endif
LinkList.c源码
#include"LinkList.h"
LinkList* Init_LinkList(){
LinkList* list = (LinkList*)malloc(sizeof(LinkList));
list->head.next = NULL;
list->size = 0;
return list;
}
void Insert_LinkList(LinkList* list, int pos, LinkNode* data){
if (list == NULL){
return;
}
if (data == NULL){
return;
}
if (pos < 0 || pos > list->size){
pos = list->size;
}
LinkNode* pCurrent = &(list->head);
for (int i = 0; i < pos;i++){
pCurrent = pCurrent->next;
}
data->next = pCurrent->next;
pCurrent->next = data;
list->size++;
}
void Remove_LinkList(LinkList* list, int pos){
if (list == NULL){
return;
}
if (pos < 0 || pos >= list->size){
return;
}
LinkNode* pCurrent = &(list->head);
for (int i = 0; i < pos;i++){
pCurrent = pCurrent->next;
}
pCurrent->next = pCurrent->next->next;
list->size--;
}
int Find_LinkList(LinkList* list, LinkNode* data, COMPARENODE compare){
if (list == NULL){
return -1;
}
if (data == NULL){
return -1;
}
LinkNode* pCurrent = list->head.next;
int index = 0;
int flag = -1;
while (pCurrent != NULL ){
if (compare(pCurrent, data) == 0){
flag = index;
break;
}
pCurrent = pCurrent->next;
index++;
}
return flag;
}
int Size_LinkList(LinkList* list){
return 0;
}
void Print_LinkList(LinkList* list, PRINTNODE print){
if (list == NULL){
return;
}
LinkNode* pCurrent = list->head.next;
while (pCurrent != NULL){
print(pCurrent);
pCurrent = pCurrent->next;
}
}
void FreeSpace_LinkList(LinkList* list){
if (list == NULL){
return;
}
free(list);
}
使用实例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LinkList.h"
typedef struct PERSON{
LinkNode node;
char name[64];
int age;
}Person;
void MyPrint(LinkNode* data){
Person* p = (Person*)data;
printf("Name:%s Age:%d\n",p->name,p->age);
}
int MyCompare(LinkNode* node1, LinkNode* node2){
Person* p1 = (Person*)node1;
Person* p2 = (Person*)node2;
if (strcmp(p1->name,p2->name) == 0 && p1->age == p2->age){
return 0;
}
return -1;
}
int main(void){
LinkList* list = Init_LinkList();
Person p1, p2, p3, p4, p5;
strcpy(p1.name, "aaa");
strcpy(p2.name, "bbb");
strcpy(p3.name, "ccc");
strcpy(p4.name, "ddd");
strcpy(p5.name, "eee");
p1.age = 10;
p2.age = 20;
p3.age = 30;
p4.age = 40;
p5.age = 50;
Insert_LinkList(list, 0, (LinkNode*)&p1);
Insert_LinkList(list, 0, (LinkNode*)&p2);
Insert_LinkList(list, 0, (LinkNode*)&p3);
Insert_LinkList(list, 0, (LinkNode*)&p4);
Insert_LinkList(list, 0, (LinkNode*)&p5);
Print_LinkList(list, MyPrint);
Remove_LinkList(list, 2);
printf("---------------\n");
Print_LinkList(list, MyPrint);
Person findP;
strcpy(findP.name, "bbb");
findP.age = 20;
int pos = Find_LinkList(list, (LinkNode*)&findP, MyCompare);
printf("位置:%d\n",pos);
FreeSpace_LinkList(list);
system("pause");
return 0;
}
|