单链表实现学生信息管理系统

  • 头文件
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count = 0;
typedef  struct {
    char  no[8];      //8位学号
    char  name[20];  //姓名
    int score;       //成绩
}  Student;


typedef  struct  Node {
    Student  data;          //数据域
    struct  Node* next;    //指针域
}  Node;
Node* CreateLinkList()//创建指针链表
{
    Node* headNode = ( Node*)malloc(sizeof( Node));//申请内存空间
    //表头:做差异化处理 数据域data不做初始化
    if (!headNode)
    {
       
        printf_s("内存分配失败!\n");
        exit(0);
    }
    headNode->next = NULL;
    return headNode;

}
Node* CreateNode(Student data)  //创建结点
{
    
    Node* newNode = (Node*)malloc(sizeof(Node));/*申请内存空间*/
    if (!newNode)
    {
        printf_s("内存分配失败!\n");
        exit(0);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
/*插入信息*/
void InsertNodeByHead(Node* headNode, Student data)//头插法
{
    Node* newNode = CreateNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
    count++;

}
/*删除信息*/
void DeleteAppoinNode(Node* headNode, char *name)
{
    Node* posNode = headNode->next;

    Node* posFrontNode = headNode;
    if (posNode == NULL)
    {
        printf_s("信息为空,无法删除!\n");
    }

    while (strcmp(posNode->data.name,name))
    {
        posFrontNode = posNode;
        posNode = posFrontNode->next;
        if (posNode == NULL)
        {
            printf_s("信息为空,无法删除!\n");
        }
    }
    posFrontNode->next = posNode->next;
    free(posNode);
}
/*查找信息*/
Node* SearchInfoByData(Node* headNode, char* name)
{
    Node* MovePtr = headNode->next;
    if (MovePtr == NULL) {
        return NULL;
    }
    while (strcmp(MovePtr->data.name, name))
    {
        MovePtr = MovePtr->next;
    }
    return MovePtr;
}


//链表存储
void ReadInfoFromFile(Node* headNode, char *filename)
{
    //1.Open files
    FILE* fp;
    Student data;
    fp = fopen(filename, "r");
    //2.Read files
    while (fscanf(fp,"%s\t%s\t%d",data.no,data.name,&data.score) !=EOF)   
    {
        InsertNodeByHead(headNode, data);
    }
    //3.Close files 
    fclose(fp);
    
}

//链表的读取
void WriteInfoFromFile(Node* headNode, char* filename)
{
    FILE* fp;
    fp = fopen(filename, "w");
    Node* pMove = headNode->next;
    while (pMove)
    {
        fprintf_s(fp, "%s\t%s\t%d", pMove->data.no, pMove->data.name, pMove->data.score);
        pMove = pMove->next;
    }
    fclose(fp);
}


//打印链表
void PrintList(Node* headNode)
{
    Node*pMove = headNode->next;
    //涉及到数据的处理
    printf("学号\t姓名\t成绩\n");
    while (pMove)
    {
        printf("%s\t%s\t%d\n", pMove->data.no, pMove->data.name, pMove->data.score);
        pMove = pMove->next;

    }
    printf("共有%d名学生\n", count);
    printf("\n");

}



  • 源码
#include"MYLIST.h"
void Menu()  /*菜单界面*/
{
    printf_s("**********************【学生信息管理系统】**********************\n");
    printf_s("*                     【   0.退出系统   】                     *\n");
    printf_s("*                     【   1.录入信息   】                     *\n");
    printf_s("*                     【   2.删除信息   】                     *\n");
    printf_s("*                     【   3.查找信息   】                     *\n");
    printf_s("*                     【   4.修改信息   】                     *\n");
    printf_s("*                     【   5.显示信息   】                     *\n");
    printf_s("****************************************************************\n");
  
}
Node* List = CreateLinkList();
void InputChoice()/*用户交互*/
{
    int choice;
    Student data;
  
    Node* pMove = NULL;
    printf_s("请输入选项:\n");
    scanf("%d",&choice);
    getchar();       //用于接收缓冲区中的回车
    switch (choice)
    {
    case 0:
        printf_s("已退出\n");
        system("pause");
        exit(0);
        break;
    case 1:
        printf_s("*                     【   1.录入信息   】                     *\n");
        //插入链表
        printf("请输入学生学号、姓名、成绩\n");
        fflush(stdin);//清除缓冲区
        scanf("%s%s%d", data.no, data.name, &data.score);
        getchar();
        InsertNodeByHead(List, data);

        break;
    case 2:
        printf_s("*                     【   2.删除信息   】                     *\n");
        printf_s("请输入要删除学生的姓名:\n");
        scanf("%s", data.name);
        getchar();
        DeleteAppoinNode(List, data.name);
        printf_s("删除成功!\n");
        count--;
        break;
    case 3:
        printf_s("*                     【   3.查找信息   】                     *\n");
        printf_s("请输入要查询的学生姓名:\n");
        scanf("%s", data.name);
       
        pMove = SearchInfoByData(List, data.name);
        if (pMove == NULL)
        {
            printf_s("未找到相关信息!\n");
            system("pause");
        }
        else
        {
            printf("学号\t姓名\t成绩\n");
            printf("%s\t%s\t%d\n", pMove->data.no, pMove->data.name, pMove->data.score);
        }
        break;
    case 4:
        printf_s("*                     【   4.修改信息   】                     *\n");
        printf("请输入修改学生的学号");
        scanf("%s", data.no);
        DeleteAppoinNode(List, data.no);
        printf("请输入你想添加的学生学号、姓名、成绩\n");
        scanf("%s%s%d\n", data.no, data.name, &data.score);
        getchar();
        InsertNodeByHead(List, data);
        break;
   
    case 5:
        printf_s("*                     【   5.显示信息   】                     *\n");
        PrintList(List);
    default:
        printf("选择错误,重新输入\n");
        system("pause");
        break;
    }
    
}
int main(void)
{
    while (1)
    {
         Menu();
        InputChoice();
        system("pause");
        system("cls");//清屏
    }

    system("pause");
    return 0;

}
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读