博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
内核链表学生管理系统,strcpy函数实现将数组指针赋值给数组
阅读量:4299 次
发布时间:2019-05-27

本文共 3524 字,大约阅读时间需要 11 分钟。

  • strcpy函数实现将数组指针赋值给数组。
  • 其中的kernel_list.h是从内核中提取出来的,下载地址:链接:https://pan.baidu.com/s/1vqkXia_m6ChFfW6p2cBQSQ
#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/

你可能感兴趣的文章
Hive进阶总结(听课总结)
查看>>
大数据领域两大最主流集群管理工具Ambari和Cloudera Manger
查看>>
Sqoop往Hive导入数据实战
查看>>
Mysql到HBase的迁移
查看>>
Sqoop import进阶
查看>>
Hive语句是如何转化成MapReduce任务的
查看>>
Hive创建table报错:Permission denied: user=lenovo, access=WRITE, inode="":suh:supergroup:rwxr-xr-x
查看>>
Hive执行job时return code 2排查
查看>>
hive常用函数及数据结构介绍
查看>>
Hive面试题干货(亲自跟着做了好几遍,会了的话对面试大有好处)
查看>>
力扣题解-230. 二叉搜索树中第K小的元素(递归方法,中序遍历解决)
查看>>
力扣题解-123. 买卖股票的最佳时机 III(动态规划)
查看>>
java中ThreadLocal类的使用
查看>>
java中数组长度为零和为空的区别
查看>>
图解eclipse 查看原始类出现The jar file rt.jar has no source attachment
查看>>
JVM堆内存设置原理
查看>>
约瑟夫问题(java实现)
查看>>
Java 中int、String的类型转换
查看>>
java实现9大排序算法
查看>>
一句话总结java23种设计模式
查看>>