本文共 3524 字,大约阅读时间需要 11 分钟。
#include#include "kernel_list.h"#include #include struct node{ char name[5]; //数据域 char id[10]; int age; struct list_head list;//地址域 struct list_head *next, *prev;};//插入节点 头插int inser_node(struct list_head *head,const char *name,const char *id,const int age){ //往头节点中加入新节点 struct node *new = malloc(sizeof(struct node)); strcpy(new->name,name); strcpy(new->id,id); new->age = age; new->list.next = NULL; new->list.prev = NULL; //list_add(struct list_head *new, struct list_head *head) list_add(&new->list,head); }//尾插 int inser_node_tail(struct list_head *head,const char *name,const char *id,const int age){ struct node *new = malloc(sizeof(struct node)); strcpy(new->name,name); strcpy(new->id,id); new->age = age; new->list.next = NULL; new->list.prev = NULL; //list_add_tail(struct list_head *new, struct list_head *head) list_add_tail(&new->list,head); }//获取链表的长度int get_len(struct list_head *head){ //计算节点的个数 struct list_head *pos = head->next; int len=0; while(pos != head) { len++; pos = pos ->next; } printf("len=%d\n",len); }void show_list(struct list_head *head){ //遍历结构体中数据 struct list_head *pos = head->next; while(pos != head) { struct node *p_node = list_entry(pos,struct node, list); //求出大结构体的地址 printf("p_node->name=%s\n",p_node->name); printf("p_node->id=%s\n",p_node->id); printf("p_node->data=%d\n",p_node->age); pos = pos ->next; }}//修改链表中的数据void change_data(struct list_head *head,const char *name, const char *id, int age){ struct list_head *pos = head->next; while(pos != head) { struct node *p_node = list_entry(pos,struct node,list); //求出大结构体的地址 if(!strcmp(p_node->name, name)) { strcpy(p_node->id,id); p_node->age = age; } pos = pos ->next; } }//姓名修改void del_node(struct list_head *head, const char *name){ struct list_head *pos = head->next; while(pos != head) { struct node *p_node = list_entry(pos,struct node,list); //求出大结构体的地址 if(!strcmp(p_node->name, name) )//找到需要删除的数据 { //删除数据 list_del(pos); //释放空间 free(p_node); return ; } pos = pos ->next; } }//利用系统提供的安全遍历方式去 删除数据 void del_node_safe(struct list_head *head,const char *name){ //遍历链表 struct list_head *pos = head; struct list_head *tmp = head; list_for_each_safe(pos,tmp,head) //循环里面已经帮助我们去移动 pos 指针 { struct node *p_node = list_entry(pos,struct node,list); //求出大结构体的地址 if(!strcmp(p_node->name, name)) //找到需要删除的数据 { //删除数据 list_del(pos); //释放空间 free(p_node); } } }int main(){ //创建链表的头节点 struct list_head *head = malloc(sizeof(struct list_head)); INIT_LIST_HEAD(head); while(1) { printf("1.添加学生信息 2.查看学生信息 3.修改信息(按姓名) 4.删除学生信息\n"); int a=0; scanf("%d",&a); switch(a) { case 1: { printf("请输入姓名:\n"); char name[5]; scanf("%s",&name); printf("请输入学号:\n"); char id[10]; scanf("%s",&id); printf("请输入年龄:\n"); int age=0; scanf("%d",&age); inser_node_tail(head,name,id,age); break; } case 2: { show_list(head); break; } case 3: { printf("请输入要学生的姓名修改 \n"); char name[5]; scanf("%s",&name); printf("输入改学生修改的 学号,年龄(用空格隔开)"); char id[10]; char age; scanf("%s %d",&id,&age); change_data(head,name,id,age); break; } case 4: { printf("请输入要删除的学生姓名 \n"); char name[5]; scanf("%s",&name); del_node_safe(head,name); break; } } }}
转载地址:http://qupws.baihongyu.com/