基于昨天代码增加增删改查功能
zy.h
#ifndef ZY_H
#define ZY_H
#define MAX 100 //最大容量
//定义学生类型
struct Stu
{char name[20];int age;double score;
};
//定义班级类型
struct Class
{struct Stu student[MAX]; //存放学生的容器int size; //实际人数
};
int *create(int size);
void input(struct Class *ptr, int size); //录入函数
void sort(struct Class *ptr, int size); //降序排序函数
void maxandmin(struct Class *ptr, int size); //输出最好和最差的学生
void output(struct Class *ptr, int size); //输出学生
void destroy(struct Class *ptr); //释放内存的函数int insert(struct Class *ptr, int size,int e); //增int delete (struct Class *ptr, int size, int e); //删int update(struct Class *ptr, int e); //改int search(struct Class *ptr, int size, int e); //查
int empty(struct Class *ptr );
int full(struct Class *ptr );
#endif
zy.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100 //最大容量
//定义学生类型
struct Stu
{char name[20];int age;double score;
};
//定义班级类型
struct Class
{struct Stu student[MAX]; //存放学生的容器int size; //实际人数
};
int empty(struct Class *ptr)
{return ptr->size == 0;
}
int full(struct Class *ptr)
{return ptr->size == MAX;
}
int *create(int size)
{struct Class *ptr = (struct Class **)malloc(sizeof(struct Class *) * size);if (ptr == NULL){printf("申请失败\n");return NULL;}memset(ptr, 0, sizeof(int) * size);ptr->size = size;return ptr;
}
void input(struct Class *ptr, int size) //录入函数
{if (ptr == NULL){printf("申请失败\n");return;}for (int i = 0; i < ptr->size; i++){printf("请输入第%d个学生的姓名,年龄,成绩(空格分开):", i + 1);scanf("%s %d %lf", ptr->student[i].name, &ptr->student[i].age, &ptr->student[i].score);}
}
void sort(struct Class *ptr, int size) //降序排序函数
{if (ptr == NULL){printf("申请失败\n");return;}for (int i = 1; i < ptr->size; i++){for (int j = 0; j < ptr->size - i; j++){if (ptr->student[j].score < ptr->student[j + 1].score){struct Stu temp = ptr->student[j];ptr->student[j] = ptr->student[j + 1];ptr->student[j + 1] = temp;}}}
}
void maxandmin(struct Class *ptr, int size) //输出最好和最差的学生
{if (ptr == NULL){printf("申请失败\n");return;}int max = 0, min = 0;for (int i = 1; i < ptr->size; i++){if (ptr->student[max].score < ptr->student[i].score){max = i;}if (ptr->student[min].score > ptr->student[i].score){min = i;}}printf("最好的学生的姓名=%s,年龄=%d,成绩=%.2lf:\n", ptr->student[max].name, ptr->student[max].age, ptr->student[max].score);printf("最差的学生的姓名=%s,年龄=%d,成绩=%.2lf:\n", ptr->student[min].name, ptr->student[min].age, ptr->student[min].score);
}
void output(struct Class *ptr, int size) //输出学生
{if (ptr == NULL){printf("申请失败\n");return;}printf("输出学生信息:\n");for (int i = 0; i < ptr->size; i++){printf("第%d个学生的姓名=%s,年龄=%d,成绩=%.2lf:\n", i + 1, ptr->student[i].name, ptr->student[i].age, ptr->student[i].score);}
}
void destroy(struct Class *ptr) //释放内存的函数
{free(ptr);
}
int insert(struct Class *ptr, int size, int e)
{//判断逻辑if (NULL == ptr || full(ptr) || e < 0 || e > ptr->size){printf("插入失败\n");return -1;}//腾空逻辑for (int i = ptr->size - 1; i >= e; i--){ptr->student[i + 1] = ptr->student[i]; //将前面的元素后移}printf("请输入添加学生的姓名,年龄,成绩(空格分开):");scanf("%s %d %lf", ptr->student[e].name, &ptr->student[e].age, &ptr->student[e].score);ptr->size++;return 0;
}int delete (struct Class *ptr, int size, int e)
{if (NULL == ptr || empty(ptr) || e < 0 || e >= ptr->size){printf("删除失败\n");return -1;}printf("删除%d位学生\n", e + 1);for (int i = e; i < ptr->size - 1; i++){ptr->student[i] = ptr->student[i + 1];}ptr->size--;return 0;
}int update(struct Class *ptr, int e)
{if (NULL == ptr || e < 0 || e >= ptr->size || empty(ptr)){printf("修改失败\n");return -1;}printf("修改%d位学生\n", e + 1);printf("请输入修改学生的姓名,年龄,成绩(空格分开):");scanf("%s %d %lf", ptr->student[e].name, &ptr->student[e].age, &ptr->student[e].score);return 0;
}int search(struct Class *ptr, int size, int e)
{if (NULL == ptr || empty(ptr)){printf("查找失败\n");return -1;}printf("查找\n");int count = 0;for (int i = 0; i < ptr->size; i++){if (ptr->student[i].score == e){printf("找到该学生的姓名=%s,年龄=%d,成绩=%.2lf:\n", ptr->student[i].name, ptr->student[i].age, ptr->student[i].score);count++;}}if (count == 0){printf("没找到\n");}return 0;
}
zymain.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zy.h"int main(int argc, char const *argv[])
{int size = 0;printf("请输入你要输入的人数:");scanf("%d", &size);struct Class *P = create(size);//调用录入函数input(P, size);//降序排序函数sort(P, size);//输出最好和最差的学生maxandmin(P, size);//输出学生output(P, size);//增insert(P, size, 1);//输出学生output(P, size);//删delete (P, size, 2);//输出学生output(P, size);//改update(P, 1);//输出学生output(P, size);//查search(P, size, 90);//释放内存的函数destroy(P);P = NULL;return 0;
}
链表的相关操作
seq_lis.h
#ifndef SEQLIST_H
#define SEQLIST_H
#include<myhead.h>
#define MAX 20
typedef int datatype;typedef struct
{datatype data[MAX];int len;
}SeqList,*SeqListPtr;SeqListPtr list_create();//创建
int list_empty(SeqListPtr L);//判空
int list_full(SeqListPtr L);//判满
int list_add(SeqListPtr L,datatype e);//增加
int list_insert_pos(SeqListPtr L,int pos,datatype e);//插入
void list_show(SeqListPtr L);//输出
int list_delete_pos(SeqListPtr L,int pos);//删除
int list_search_value(SeqListPtr L, datatype e);//查找
int list_update_pos(SeqListPtr L, int pos, datatype e);//更新(位置)
int list_update_value(SeqListPtr L, datatype old_e, datatype new_e);//更新(值)
#endif
seq_list.c
#include"seqlist.h"
//创建
SeqListPtr list_create()
{SeqListPtr L = (SeqListPtr)malloc(sizeof(SeqList));if(L == NULL){printf("申请失败\n");return NULL;}memset(L->data,0,sizeof(L->data));L->len=0;printf("创建成功\n");return L;
}
//判空
int list_empty(SeqListPtr L)
{return L->len==0;
}
//判满
int list_full(SeqListPtr L)
{return L->len==MAX;
}
//增加
int list_add(SeqListPtr L, datatype e)
{if(NULL==L || list_full(L)){printf("添加失败\n");return -1;}L->data[L->len] = e;L->len++;printf("添加成功\n");return 0;
}
//输出
void list_show(SeqListPtr L)
{if( L == NULL || list_empty(L)){printf("遍历失败\n");return;}printf("元素分别为:\n");for(int i =0;i<L->len;i++){printf("%d\t",L->data[i]);}printf("\n");
}
//插入
int list_insert_pos(SeqListPtr L,int pos,datatype e)
{if(NULL == L|| list_full(L)||pos<0||pos>L->len){printf("插入失败\n");return -1;}for( int i = L->len-1;i>=pos;i--){L->data[i+1] = L->data[i];}L->data[pos] = e;L->len++;printf("插入成功\n");return 0;
}
//删除
int list_delete_pos(SeqListPtr L,int pos)
{if(NULL == L|| list_empty(L)||pos<0||pos>=L->len){printf("删除失败\n");return -1;}for( int i=pos-1;i<L->len;i++){L->data[i] = L->data[i+1];}L->len--;printf("删除成功\n");return 0;
}
//查找
int list_search_value(SeqListPtr L, datatype e)
{if (L== NULL||list_empty(L)) {printf("查找失败\n");return -1;}
for (int i = 0; i < L->len; i++)
{if (e == L->data[i]){printf("找到了是第%d个值为=%d\n",i+1,L->data[i]);return i;}
}
printf("没有找到\n");
}
//更新(位置)
int list_update_pos(SeqListPtr L, int pos, datatype e)
{if (L==NULL || pos>=L->len||pos<0||list_empty(L)){printf("修改失败\n");return -1;}L->data[pos] = e;printf("修改成功\n");return 0;
}
//更新(值)
int list_update_value(SeqListPtr L, datatype old_e, datatype new_e)
{if (L==NULL || list_empty(L)){printf("修改失败\n");return -1;}int num = -1;for (int i = 0; i < L->len; i++){ if (old_e == L->data[i]){num = i;}}if ( num == -1){printf("没有这个值\n");return -1;}list_update_pos(L, num, new_e);return 0;
}
main.c
#include"seqlist.h"
int main(int argc, const char *argv[])
{SeqListPtr L = list_create();if(L == NULL){return -1;}list_add(L,1);list_add(L,2);list_add(L,3);list_add(L,4);list_add(L,5);list_insert_pos(L,0,9);list_insert_pos(L,4,9);list_insert_pos(L,7,9);list_show(L);list_delete_pos(L,2);list_show(L);list_search_value(L,3);list_update_pos(L,1,7);list_show(L);list_update_value(L,4,10);list_show(L);return 0;
}