整个函数的功能:创建链表的及打印链表内容,查找链表中的某个值,删除链表中的某个值
主函数内容:
文件名:main.c
#include "person.h"
void print_jack(struct person *curr)
{
struct jack *t = (struct jack *)curr;
printf("%s eat %s\n",t->name,t->food);
}
void print_fuck(struct person *curr)
{
struct fuck *t = (struct fuck *)curr;
printf("%s eat %s\n",t->name,t->food);
}
void show(struct person *head)
{
if(NULL == head)
{
return;
}
while(head)
{
if(0 == strcmp(head->labe, "jack"))
{
travers(head,print_jack);
}
else
{
travers(head,print_fuck);
}
head = head->next;
}
}
int main()
{
int i = 5;
char jack[5][10] = {"jack_1","jack_2","jack_3","jack_4","jack_5"};
char jfd[10] = {"orange"};
char fuck[5][10] = {"fuck_1","fuck_2","fuck_3","fuck_4","fuck_5"};
char ffd[10] = {"apple"};
struct person *head = NULL;
while(i--)
{
struct jack *j = (struct jack *)malloc(sizeof(struct jack));
strcpy(j->name,jack[i]);
strcpy(j->food,jfd);
j->p.labe = "jack";
head_list(&head,&j->p);
struct fuck *f = (struct fuck *)malloc(sizeof(struct fuck));
strcpy(f->name,fuck[i]);
strcpy(f->food,ffd);
f->p.labe = "fuck";
head_list(&head,&f->p);
}
show(head);
FIND(head,"fuck_1");
printf("=============\n");
delete(head,"jack_2");
show(head);
return 0;
}
头文件中函数的定义:
文件名:person.c
#include "person.h"
void head_list(struct person **head, struct person *n)
{
n->pre = NULL;
n->next = *head;
if(NULL != *head)
{
(*head)->pre = n;
}
*head = n;
}
void travers(struct person *head, void (*fp)(struct person *))
{
fp(head);
}
头文件内容:
文件名:person.h
#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct person
{
char *labe;
struct person *pre;
struct person *next;
};
struct jack
{
struct person p;
char name[10];
char food[10];
};
struct fuck
{
struct person p;
char name[10];
char food[10];
};
void head_list(struct person **head, struct person *n);
void travers(struct person *head,void (*fp)(struct person *));
void FIND(struct person *head,void *key);
void delete(struct person **head,char *key);
查找函数内容
文件名:find.c
#include "person.h"
void FIND(struct person *head,void *key)
{
int ret = 0;
while(head)
{
if(0 == strcmp(head->labe,"jack"))
{
if(0 == strcmp(((struct jack *)head)->name, (char *)key))
{
printf("Find jack !\n");
ret += 1;
}
}
else
if(0 == strcmp(head->labe,"fuck"))
{
if(0 == strcmp(((struct fuck *)head)->name, (char *)key))
{
printf("Find fuck !\n");
ret += 1;
}
}
head = head->next;
}
if(0 == ret)
{
printf("NO,Find !\n");
}
}
删除函数内容
文件名:delete.c
#include "person.h"
void delete(struct person **head,void *key)
{
int ret = 0;
struct person *past = *head;
struct person *curr = past;
while(curr)
{
if(0 == strcmp(curr->labe,"jack"))
{
if(0 == strcmp(((struct jack *)curr)->name, (char *)key))
{
if(curr == *head) //如果是第一个
{
(*head)->next->pre = (*head)->pre;
*head = (*head)->next;
}
if(curr->next == NULL) //如果是最后一个
{
curr->pre->next = NULL;
}
else
{
curr->next->pre = past;
past->next = curr->next;
}
free(curr);
curr = NULL;
ret += 1;
}
}
else
if(0 == strcmp(curr->labe,"fuck"))
{
if(0 == strcmp(((struct fuck *)curr)->name, (char *)key))
{
if(curr == *head)
{
(*head)->next->pre = (*head)->pre;
(*head) = (*head)->next;
}
if(curr->next == NULL)
{
curr->pre->next = NULL;
}
else
{
curr->next->pre = past;
past->next = curr->next;
}
free(curr);
curr = NULL;
ret += 1;
}
}
//curr = curr->next;
if(0 == ret)
{
past = curr;
curr = curr->next;
}
}
}