#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _employee{
char name[32];
unsigned char age;
}Employee;//定义员工结构体
typedef struct _node{
void*data;
struct _node *next;
}Node;//定义节点结构体
typedef struct _linkedList{
Node *head;
Node *tail;
Node *current;
}LinkedList;//定义链表结构体
typedef void(*DISPLAY)(void*);//定义函数指针
typedef int(*COMPARE)(void*,void*);//定义函数指针
void initializeList(LinkedList*);//链表初始化函数
void addHead(LinkedList*,void*);//给链表添加头节点
void addTail(LinkedList*,void*);//给链表添加尾节点
void delete(LinkedList*,Node*);//删除链表中的节点
Node *getNode(LinkedList*,COMPARE,void*);//获取节点结构体指针
void displayLinkedList(LinkedList*,DISPLAY);//显示链表节点
int compareEmployee(Employee *e1,Employee* e2);//比较链表中的数据,相同返回0,不同返回1
void displayEmployee(Employee* employee);//显示员工结构体
int main() {
LinkedList linkedList;
Employee *samuel=(Employee*)malloc(sizeof (Employee));
strcpy(samuel->name,"Samuel");
samuel->age=32;
Employee *sally=(Employee*)malloc(sizeof (Employee));
strcpy(sally->name,"Sally");
sally->age=28;
Employee *susan=(Employee*)malloc(sizeof (Employee));
strcpy(susan->name,"Susan");
susan->age=45;
initializeList(&linkedList);
addHead(&linkedList,samuel);
addHead(&linkedList,sally);
addHead(&linkedList,susan);
addTail(&linkedList,susan);
// Node* node=getNode(&linkedList,(int(*)(void*,void*))compareEmployee,susan);
//Node* node1=getNode(&linkedList,(int(*)(void*,void*))compareEmployee,sally);
//delete(&linkedList,node);
//delete(&linkedList,node1);
//addHead(&linkedList,susan);
//addHead(&linkedList,samuel);
displayLinkedList(&linkedList,(DISPLAY)displayEmployee);
return 0;
}
int compareEmployee(Employee *e1,Employee* e2){
return strcmp(e1->name,e2->name);
}
void displayEmployee(Employee* employee){
printf("%s\t%d\n",employee->name,employee->age);
}
void initializeList(LinkedList* list){//链表结构体体成员初始化
list->head=NULL;//链表头节点初始化
list->tail=NULL;//链表尾节点初始化
list->current=NULL;//链表当前节点初始化
}
void addHead(LinkedList* list,void* data){//给链表添加新的头节点
Node* node=(Node*)malloc(sizeof(Node));//给新节点申请空间
node->data=data;//将员工结构体指针添加到新节点的data段
if(list->head==NULL){//如果刚开始没有节点,即链表为空
list->tail=node;//则将新节点添加到链表的尾部
node->next=NULL;//将新节点的下一节点设置为空
} else{
node->next=list->head;//否则将当前链表的头节点添加到新节点的next成员变量中
}
list->head=node;//将新节点添加到链表表头
}
void addTail(LinkedList* list,void* data){//给链表添加新的尾节点
Node* node=(Node*)malloc(sizeof (Node));//为新节点申请空间
node->data=data;//将员工结构体指针添加到新节点的data段
node->next=NULL;//将新节点的下一节点设置为空
if(list->head==NULL){//如果刚开始没有节点,即链表为空
list->head=node;//则将新节点设置为链表的头节点
} else{
list->tail->next=node;//否则将新节点设置为当前链表的尾部节点的下一节点
}
list->tail=node;//将当前链表的尾节点设置为新节点
}
Node* getNode(LinkedList *list,COMPARE compare,void* data){//查找节点
Node* node=list->head;//将新节点设置为链表头节点
while(node!=NULL){//如果节点不为空
if(compare(node->data,data)==0){//则比较链表中的data数据段,相同则返回节点
return node;
}
node=node->next;//如果不同,继续寻找下一节点
}
return NULL;//如果节点为空,则返回空节点
}
void delete(LinkedList* list,Node* node){//删除节点
if(node==list->head){//如果要删除的节点为链表头节点
if(list->head->next==NULL){//且当前链表仅有一个节点
list->head=list->tail=NULL;//则将链表置空
} else{
list->head=list->head->next;//否则,将下一节点置为当前链表的头结点
}
} else{//如果不是要删除的节点不是当前链表的头结点
Node* temp=list->head;//将当前链表的头节点设置为新节点
while(temp!=NULL&&temp->next!=node){//当链表不为空,且当前节点的下一节点不是要删除的节点时
temp=temp->next;//继续寻找下一节点是不是要删除的节点
}
if(temp!=NULL){
temp->next=node->next;//将要删除的节点的下一节点赋值给要删除节点的上一节点的next指针
}
}
free(node);//释放要删除节点的空间
}
void displayLinkedList(LinkedList* list,DISPLAY display){//显示链表
printf("\nLinked List\n");
Node *current=list->head;
while(current!=NULL){
display(current->data);
current=current->next;
}
}
数据结构:单向链表
最新推荐文章于 2025-12-10 16:15:26 发布
该博客展示了如何使用C语言创建和操作链表。它定义了 Employee 结构体,用于存储员工信息,并创建了一个 Node 结构体来构建链表。此外,还实现了链表的初始化、添加头节点和尾节点、删除节点、获取节点以及显示链表内容的功能。通过示例代码,博主演示了如何添加和显示员工数据,并提供了比较和显示 Employee 的函数。
1012

被折叠的 条评论
为什么被折叠?



