C语言链表(创建、插入、删除、冒泡/插入排序、折半查找)实现学生信息管理系统

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32958797/article/details/84843319
**前言** C基本都忘了,本来想找一个直接用的,但是多多少少都会有些问题,所以只能再自习回顾下,也参考了一些博客教程,最后完成图示功能。 **效果图** ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181205205228103.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMyOTU4Nzk3,size_16,color_FFFFFF,t_70) ![在这里插入图片描述](https://img-blog.csdnimg.cn/20181205205642329.png) **代码**
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#include <windows.h>

# define Esc 27
# define LEN sizeof(struct student)

struct student{
    int num;
    int score;
    struct student *next;
};

int n = 0;//全局变量,记录学生记录数

//函数声明
struct student *create();
struct student *insert(struct student *head);
void bubblesort(struct student *head);
void insert_sort(struct student *head);
void GetPoint(struct student *head,int i);
void change(struct student *list);
void del(struct student *list);
int GetElem(struct student *head,int i);
void bin_search(struct student *head);
void menu();
void goon();

//系统主界面
void menu()
{
	printf("\n\n\t\t\t	    &欢迎使用学生成绩管理系统&     ");
	printf("\n\n\t\t		1.新建学生信息表	");
	printf("\n\n\t\t		2.冒泡排序(学号)输出学生信息		");
	printf("\n\n\t\t		3.插入排序(成绩)输出学生信息		   ");
	printf("\n\n\t\t		4.折半法查询学号并显示对应成绩信息");
	printf("\n\n\t\t		5.删除学生信息  ");
	printf("\n\n\t\t		6.修改学生信息 ");
	printf("\n\n\t\t		7.添加学生信息	");
	printf("\n\n\t\t		ESC.退出系统    ");
	printf("\n\n\t\t	---------------------------------------------");
	printf("\n\t\t\t\t请输入您的选择项(1-7):");
}
//创建链表
struct student *create()
{
    struct student *head,*node,*end;
    head = (struct student*)malloc(LEN);
    end = head;
    int i,number;
    printf("\t请输入要创建的学生信息个数:");
    scanf("%d",&number);
    for(i=0;i<number;i++){
        node=(struct student*)malloc(LEN);
        printf("\t请输入学号:");
        scanf("%d",&node->num);
        printf("\t请输入英语成绩:");
        scanf("%d",&node->score);
        end->next=node;
        end=node;
        n++;
    }
    end->next=NULL;
    return head;
};
//删除学生信息
void del(struct student *head){
    int s,i=0;
    printf("\t删除第几个学生的信息:");
    scanf("%d",&s);
    struct student *t=head,*in;
    while (i<s&&t!=NULL){
		in=t;
		t=t->next;
		i++;
	}
	if(t!=NULL){
        n--;
		in->next=t->next;
		free(t);
	}
	else{
		printf("节点不存在");
	}
}
//修改学生信息
void change(struct student *head){
    int s,i=0;
    printf("\t修改第几个学生的信息:");
    scanf("%d",&s);
    struct student *t=head;
    while(i<s&&t!=NULL){
        t=t->next;
        i++;
    }
    if(t != NULL){
        printf("\t请输入修改学号:");
        scanf("%d",&t->num);
        printf("\t请输入修改英语成绩:");
        scanf("%d",&t->score);
    }else{
        printf("节点不存在");
    }
}
//插入学生信息
struct student *insert(struct student *head)
{
    struct student *t=head,*in;
    int i=0;
    while(i<n&&t!=NULL){
        t=t->next;
        i++;
    }
    if(t!=NULL){
        in=(struct student*)malloc(LEN);
        printf("\t请输入学号:");
        scanf("%d",&in->num);
        printf("\t请输入英语成绩:");
        scanf("%d",&in->score);
        n++;
        in->next=t->next;
        t->next=in;
    }else{
        printf("节点不存在");
    }
    return head;
};
//冒泡排序学生信息
void bubblesort(struct student *head)
{
    int count = n;//用来控制次数
    struct student *pMove;
    pMove = head->next;
    while (count > 1) {
        while (pMove->next != NULL) {
            if (pMove->num > pMove->next->num) {
                int temp1,temp2;
                temp1 = pMove->num;
                pMove->num = pMove->next->num;
                pMove->next->num = temp1;
                temp2 = pMove->score;
                pMove->score = pMove->next->score;
                pMove->next->score = temp2;
            }
            pMove = pMove->next;
        }
        count--;
        //重新移动到第一个节点
        pMove = head->next;
    }
};
//插入排序学生信息
void insert_sort(struct student *head){
    struct student *newhead, *pre, *l1, *l2;
    l1=head->next;
    newhead=l1->next;
    l1->next=NULL;
    while(newhead)
    {
       l2=newhead;
       newhead=newhead->next;
       pre=head;
       l1=head->next;
       while(l1!=NULL&&l1->score<l2->score)
       {
           pre=l1;
           l1=l1->next;
       }
       l2->next=l1;
       pre->next=l2;
    }
};
//取得链表第i个元素
int GetElem(struct student *head,int i){
    int j=0;
    struct student *p;
    p = head;
    while(p&&j<i){
        p = p->next;
        j++;
        }
    return p->num;
    }
//输出第i个元素数据
void GetPoint(struct student *head,int i){
    int j=0;
    struct student *p;
    p = head;
    while(p&&j<i){
        p = p->next;
        j++;
        }
    printf("\n学号\t英语成绩\n");
    printf("%d",p->num );
	printf("\t%d\n",p->score );
}
//折半查找学生信息
void bin_search(struct student *head){
    int low=1,high=n,mid=0,key;
    printf("\t请输入要查找学生信息的学号:");
    scanf("%d",&key);
    while(low<=high){
        mid=(low+high)/2;
        if(key==GetElem(head,mid))
        {
            GetPoint(head,mid);
            break;
        }
        else if(key<GetElem(head,mid))
            high=mid-1;
        else
            low=mid+1;
    }
}
//输出链表
void Output(struct student *head)
{
    struct student *p;
	p=head->next;
	printf("\n\n\n****************学生成绩信息表****************\n\n");
    printf("\n\t现在有%d个记录是:\n",n);
	printf("\n学号\t英语成绩\n");
	if(p!=NULL)
    {
		do
		{
			printf("%d",p->num );
			printf("\t%d\n",p->score );
			p=p->next;
		} while(p!=NULL);
    }
}
void goon()
{
    printf("按任意键继续!!!");
	getch();
}
//主程序
int main(){
    struct student *head;
    char ckey='a';
    int istate=0;//记录链表是否有数据
    do
    {
        system("cls");
        menu();
        ckey=getch();
        if(ckey=='1')
        {
            if(head!=NULL){
                head=NULL;
                n=0;
            }
            system("cls");
            head=create();
            Output(head);
            istate=1;
            goon();
        }
        else if((istate==0)&&(ckey!=Esc))
		{
			printf("\n\t错误:你必须先输入学生信息!!!");
			goon();
		}
		else if(ckey=='2')
		{
			system("cls");
			bubblesort(head);
			Output(head);
			goon();
		}
		else if(ckey=='3')
		{
			system("cls");
			insert_sort(head);
			Output(head);
			goon();
		}
		else if(ckey=='4')
		{
			system("cls");
			bubblesort(head);
			bin_search(head);
			//Output(head);
			goon();
		}
		else if(ckey=='5')
		{
			system("cls");
			del(head);
			Output(head);
			goon();
		}
		else if(ckey=='6')
		{
			system("cls");
			change(head);
			bubblesort(head);
			Output(head);
			goon();
		}
		else if(ckey=='7')
		{
			system("cls");
			insert(head);
			bubblesort(head);
			Output(head);
			goon();
		}
    }while(ckey!=Esc);
    exit(0);
}

参考文章:
c语言链表详解(超详细)
C语言链表学生信息管理系统
折半查找(C语言链表实现)

猜你喜欢

转载自blog.csdn.net/qq_32958797/article/details/84843319