目录
我使用的是单链表实现的
一、主函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "2.h"
int main(int argc, const char *argv[])
{
student *Q=create();
if(NULL == Q){
printf("创建失败\n");
return -1;
}
int n;
while(1){
jiemian();
scanf(" %d",&n);
switch(n)
{
case 0:
chan_free(Q);
Q=NULL;
exit(0);
case 1:
head(Q);
break;
case 2:
tail(Q);
break;
case 3:
pos_cha(Q);
break;
case 4:
delete_head(Q);
break;
case 5:
delete_tail(Q);
break;
case 6:
pos_shan(Q);
break;
case 7:
value(Q);
break;
case 8:
xiugai_pos(Q);
break;
case 9:
xiugai_value(Q);
break;
case 10:
paixu(Q);
break;
case 11:
show(Q);
break;
}
}
return 0;
}
二、封装函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "2.h"
//创建
student *create()
{
student *Q=(student*)malloc(sizeof(student));
if(NULL == Q){
printf("创建失败\n");
return NULL;
}
Q->len=0;
memset(Q->name, 0, sizeof(Q->name));
Q->id=0;
Q->next=NULL;
printf("创建成功\n");
return Q;
}
//判空
int empty(student *Q)
{
if(NULL == Q){
printf("链表不合法\n");
return -1;
}
return Q->next == NULL ? 1:0;
}
//界面
void jiemian()
{
printf("\t========学生管理系统========\n");
printf("\t\t1>头插\n");
printf("\t\t2>尾插\n");
printf("\t\t3>任意插入\n");
printf("\t\t4>头删除\n");
printf("\t\t5>尾删除\n");
printf("\t\t6>任意删除\n");
printf("\t\t7>按成绩查找\n");
printf("\t\t8>按位置修改成绩\n");
printf("\t\t9>按成绩修改成绩\n");
printf("\t\t10>排序\n");
printf("\t\t11>查看所有成员信息\n");
printf("\t\t0>退出\n");
printf("\t请输入:");
}
//头插
int head(student *Q)
{
if(NULL == Q){
printf("头插失败\n");
return -1;
}
student *p=(student*)malloc(sizeof(student));
if(NULL == p)
{
printf("申请失败\n");
return -2;
}
//存放数据
Q->len++;
printf("请输入第%d个学生的姓名:\n",Q->len);
scanf(" %s",p->name);
printf("请输入第%d个学生的编号:\n",Q->len);
scanf(" %d",&p->id);
printf("请输入第%d个学生的成绩:\n",Q->len);
scanf(" %f",&p->score);
p->next=NULL;
//链接头
p->next=Q->next;
Q->next=p;
printf("头插成功\n");
return 0;
}
//尾插
int tail(student *Q)
{
if(NULL == Q){
printf("尾插失败\n");
return -1;
}
student *p=(student*)malloc(sizeof(student));
if(NULL == p)
{
printf("申请失败\n");
return -2;
}
//判断是否是结尾
student *q=Q;
while(q->next != NULL)
{
q=q->next;
}
//存放数据
Q->len++;
printf("请输入第%d个学生的姓名:\n",Q->len);
scanf(" %s",p->name);
printf("请输入第%d个学生的编号:\n",Q->len);
scanf(" %d",&p->id);
printf("请输入第%d个学生的成绩:\n",Q->len);
scanf(" %f",&p->score);
p->next=NULL;
//链接尾巴
q->next=p;
printf("尾插成功\n");
return 0;
}
//遍历
void show(student *Q)
{
student *p=Q->next;
int i=0;
printf("详细信息如下:\n");
printf("\t姓名\t编号\t成绩\n");
for(i=1;i<=Q->len;i++)
{
printf("\t%s\t%d\t%.2f\n",p->name,p->id,p->score);
p=p->next;
}
}
//排序
void paixu(student *Q)
{
if(NULL == Q || empty(Q)){
printf("排序失败\n");
return ;
}
student *p=Q->next;
int i=0,k=0;
char name[MAX];
float temp;
int num;
for(i=1;i<Q->len;i++){
student *q=Q->next;
for(k=1;k<Q->len;k++){
if(q->score > q->next->score){
//成绩交换
temp=q->next->score;
q->next->score=q->score;
q->score=temp;
//编号交换
num=q->next->id;
q->next->id=q->id;
q->id=num;
//姓名交换
strcpy(name,q->next->name);
strcpy(q->next->name,q->name);
strcpy(q->name,name);
//下一个位置
}
q=q->next;
}
}
show(Q);
}
//任意插入
int pos_cha(student *Q)
{
if(NULL == Q){
printf("任意插失败\n");
return -1;
}
student *p=(student*)malloc(sizeof(student));
if(NULL == p)
{
printf("申请失败\n");
return -2;
}
//存放数据
Q->len++;
printf("请输入第%d个学生的姓名:\n",Q->len);
scanf(" %s",p->name);
printf("请输入第%d个学生的编号:\n",Q->len);
scanf(" %d",&p->id);
printf("请输入第%d个学生的成绩:\n",Q->len);
scanf(" %f",&p->score);
p->next=NULL;
//确定位置
int arr;
printf("请输入要插入位置:\n");
scanf(" %d",&arr);
arr=arr-1;
student *q=find(Q, arr);
if(q == NULL){
printf("插入位置不对\n");
free(p);
p=NULL;
Q->len--;
return -3;
}
//插入
p->next=q->next;
q->next=p;
printf("任意插入成功\n");
return 0;
}
//头删
int delete_head(student *Q)
{
if(NULL == Q || empty(Q)){
printf("头删除失败\n");
return -1;
}
student *p=Q->next;
Q->next=p->next;
free(p);
p=NULL;
Q->len--;
printf("头删除成功\n");
return 0;
}
//尾删
int delete_tail(student *Q)
{
if(NULL == Q || empty(Q)){
printf("尾删除失败\n");
return -1;
}
student *p=find(Q,Q->len-1);
student *q=p->next;
p->next=NULL;
free(q);
q=NULL;
Q->len--;
printf("尾删除成功\n");
return 0;
}
//任意删
int pos_shan(student *Q)
{
int pos;
printf("请输入要删除的位置号:\n");
scanf(" %d",&pos);
if(NULL == Q || empty(Q) || pos<0 || pos > Q->len){
printf("任意删除失败\n");
return -1;
}
if(1 == pos){
delete_head(Q);
}else{
student *p=find(Q, pos-1);
student *q=p->next;
p->next=q->next;
free(q);
q=NULL;
Q->len--;
printf("任意删除成功\n");
return 0;
}
}
//按位置查找
student *find(student *Q,int num)
{
if(NULL == Q){
printf("查找失败\n");
return NULL;
}
if(num<0 || num>Q->len){
printf("输入位置不对\n");
return NULL;
}
student *p=Q;
int i=0;
for(i=1;i<=num;i++)
{
p=p->next;
}
return p;
}
//按值查找
int value(student *Q)
{
if(NULL == Q || empty(Q)){
printf("按值查找失败\n");
return -1;
}
printf("请输入要查找的值:\n");
datatype num;
scanf(" %f",&num);
student *q=Q->next;
int i=0;
while(q != NULL){
if(q->score == num){
if(0 == i){
printf("\t姓名\t编号\t成绩\n");
}
i++;
printf("\t%s\t%d\t%.2f\n",q->name,q->id,q->score);
}
q=q->next;
}
}
//按位置修改
int xiugai_pos(student *Q)
{
if(NULL == Q){
printf("查找失败\n");
return -1;
}
printf("请输入要修改的位置号\n");
int num;
scanf(" %d",&num);
if(num<0 || num>Q->len){
printf("输入位置不对\n");
return -2;
}
datatype new_e;
printf("请输入修改值:\n");
scanf(" %f",&new_e);
student *q=Q;
int i=0;
for(i=1;i<=num;i++)
{
q=q->next;
}
q->score=new_e;
printf("按位置修改成功\n");
return 0;
}
//按值修改
int xiugai_value(student *Q)
{
if(NULL == Q || empty(Q)){
printf("按值修改失败\n");
return -1;
}
printf("请输入要查找的值:\n");
datatype num;
scanf(" %f",&num);
datatype new_e;
printf("请输入修改值:\n");
scanf(" %f",&new_e);
student *q=Q->next;
while(q != NULL){
if(q->score == num){
q->score =new_e;
}
q=q->next;
}
printf("按值修改成功\n");
return 0;
}
//释放空间
void chan_free(student *Q)
{
if(NULL == Q){
return ;
}
while(Q->next != NULL){
delete_head(Q);
}
free(Q);
Q=NULL;
printf("释放空间完成\n");
return ;
}
三、头文件
#ifndef _2_H_
#define _2_H_
#define MAX 20
typedef float datatype;
typedef struct num
{
union{
int len; //数组长度
datatype score;//存放队列的成绩
};
char name[MAX]; //存放姓名
int id; //存放编号
struct num *next;
}student;
//创建
student *create();
//判空
int empty(student *Q);
//头插
int head(student *Q);
//尾插
int tail(student *Q);
//界面
void jiemian();
//遍历
void show(student *Q);
//排序
void paixu(student *Q);
//任意插入
int pos_cha(student *Q);
//头删
int delete_head(student *Q);
//尾删
int delete_tail(student *Q);
//任意删
int pos_shan(student *Q);
//按位置查找
student *find(student *Q, int num);
//按值查找
int value(student *Q);
//按位置修改
int xiugai_pos(student *Q);
//按值修改
int xiugai_value(student *Q);
//释放空间
void chan_free(student *Q);
#endif
执行结果