
作业社区
探索学习新天地,共享知识资源!
慕运维8597106 的学生作业:
seqlist.h #ifndef __SEQLIST_H__ #define __SEQLIST_H__ #define MAX 10 struct student { char name[20]; int id; int age; }; typedef struct student data_type_t; typedef struct { data_type_t buf[MAX]; int n; } seqlist_t; extern seqlist_t *create_empty_seqlist(); extern int is_full_seqlist(seqlist_t *l); extern void insert_data_seqlist(seqlist_t *l,data_type_t data); extern void print_data_seqlist(seqlist_t *l); int is_empty_seqlist(seqlist_t *l); int delete_data_seqlist(seqlist_t *l,data_type_t data); #endif seqlist.c #include #include "seqlist.h" #include #include seqlist_t *create_empty_seqlist() { seqlist_t *p = (seqlist_t *) malloc(sizeof(seqlist_t)); if(NULL == p) { printf("malloc failed!\n"); return NULL; } memset(p,0,sizeof(seqlist_t)); p->n = 0; return p; } int is_full_seqlist(seqlist_t *l) { if(l->n >= MAX) { return 1; } return 0; } void insert_data_seqlist(seqlist_t *l,data_type_t data) { if(is_full_seqlist(l)) { printf("seqlist is full!\n"); return; } l->buf[l->n] = data; l->n++; } void print_data_seqlist(seqlist_t *l) { if(NULL != l) { for(int i = 0;i < l->n;i++) { printf("%s\t%d\t%d ",l->buf[i].name,l->buf[i].id,l->buf[i].age); printf("\n"); } } } int is_empty_seqlist(seqlist_t *l) { if(NULL == l || l->n == 0) { return 1; } return 0; } int delete_data_seqlist(seqlist_t *l,data_type_t data) { if(is_empty_seqlist(l)) { return 0; } int i = 0,j = 0; for(i = 0;i < l->n;i++) { if(l->buf[i].age != data.age){ l->buf[j] = l->buf[i]; j++; } } l->n = j; } main.c #include #include "seqlist.h" #include #include int main(int argc, const char *argv[]) { seqlist_t *p = create_empty_seqlist(); if(NULL != p) { data_type_t stu = {"zs",1,25}; insert_data_seqlist(p,stu); strcpy(stu.name,"lisi"); stu.id = 2; stu.age = 27; insert_data_seqlist(p,stu); strcpy(stu.name,"wangwu"); stu.id = 3; stu.age = 35; insert_data_seqlist(p,stu); strcpy(stu.name,"zhaoliu"); stu.id = 6; stu.age = 22; insert_data_seqlist(p,stu); strcpy(stu.name,"zhouqi"); stu.id = 7; stu.age = 24; insert_data_seqlist(p,stu); strcpy(stu.name,"liuba"); stu.id = 8; stu.age = 17; insert_data_seqlist(p,stu); strcpy(stu.name,"niu9"); stu.id = 9; stu.age = 35; insert_data_seqlist(p,stu); print_data_seqlist(p); data_type_t target; printf("请输入删除掉人员的目标年龄:"); scanf("%d",&target.age); delete_data_seqlist(p,target); printf("===删除年龄%d岁后===\n",target.age); print_data_seqlist(p); free(p); p = NULL; } return 0; } 执行结果 linux@linux:~/learn/chapter5/seqlist$ ./a.out zs 1 25 lisi 2 27 wangwu 3 35 zhaoliu 6 22 zhouqi 7 24 liuba 8 17 niu9 9 35 请输入删除掉人员的目标年龄:35 ===删除年龄35岁后=== zs 1 25 lisi 2 27 zhaoliu 6 22 zhouqi 7 24 liuba 8 17




