使用c語言單向鏈表實現簡單學生成績管理系統(1)

使用c語言單向鏈表實現簡單學生成績管理系統

鏈表是一種基本的數據結構類型,可以實現對信息的管理,實現此係統,需要定義一個管理信息的數據類型結構體,然後
對數據信息進行增刪改查即可。

本文定義一個個人成績結構體,用於存儲個人信息。

struct grade_msg
{	
	char name[16];
	char sex;
	int chinese;
	int math;
	int english;
	.... 
}

struct student_msg
{
	struct grade_msg msg;
	struct student_msg * next_node;
}

最終只需要實現一個單向鏈表實現對上述結構體的增刪改查即可。
今天先實現數據的插入,與輪詢打印查看(查看全部信息),代碼如下,後續繼續實現對個人信息的修改,查詢和刪除等。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//個人信息結構體,各科成績可自定義添加
typedef struct grade_msg
{
        char name[16];
        char sex;
        int chinese;
        int math;
        int english;

}grade_msg_t;

//鏈表結構體
typedef struct student_msg
{
        grade_msg_t msg;
        struct student_msg * next;
}student_msg_t;

//創建鏈表頭
student_msg_t * create_slist_head()
{
        student_msg_t * head;
        //申請空間
        head = (student_msg_t *)malloc(sizeof(student_msg_t));
        if(head == NULL)
        {
                printf("create slist head fail\n");
                return NULL;
        }
        //初始化
        head->next = NULL;

        return head;
}

//判斷鏈表中有無數據
//返回1爲空,0爲非空
int isnull(student_msg_t *  head)
{
        if(head->next == NULL)
                return 1;
        else
                return 0;
}

//鏈表頭添加數據
int insert_data(student_msg_t * head,grade_msg_t * msg)
{
        printf("enter insert msg\n");

        student_msg_t * new_node;

        new_node = (student_msg_t * )malloc(sizeof(student_msg_t));
        if(new_node == NULL)
        {
                printf("insert,create space fail\n");
                return -1;
        }

        strcpy(new_node->msg.name,msg->name);
        new_node->msg.sex = msg->sex;
        new_node->msg.chinese = msg->chinese;
        new_node->msg.math = msg->math;
        new_node->msg.english = msg->english;

        new_node->next = head->next;

        head->next = new_node;
        return 0;
}

//鏈表尾部刪除數據
int delete_data(student_msg_t * head,grade_msg_t * msg)
{

        return 0;
}

//打印所有信息
int printf_list(student_msg_t *  head)
{
        if(isnull(head))
        {
                printf("no student msg\n");
                return -1;
        }
        while(head->next != NULL)
        {
                head = head->next;
                printf("name:%s,sex:%c,chinese:%d,math:%d,english:%d\n",head->msg.name,head->msg.sex,head->msg.chinese,head->msg.math,head->msg.english);
        }

        return 0;

}

//修改數據
int modify_data(student_msg_t * head,char * data,int index)
{

        return 0;
}

int main(int argc, const char *argv[])
{
        student_msg_t * msg_head;

        msg_head = create_slist_head();
        if(msg_head == NULL)
        {
                printf("create student msg head fail\n ");
                return -1;
        }

        while(1)
        {
                //姓名 字符串, 性別 字符類型, 語數英 整型
                printf("please input 姓名[enter] 性別[enter] 語文[enter] 數學[enter] 英語[enter]\n");
                grade_msg_t insert_msg;
                printf("------------input start------------\n");
                scanf("%s",insert_msg.name);
                getchar();

                scanf("%c",&insert_msg.sex);
                getchar();

                scanf("%d",&insert_msg.chinese);
                getchar();

                scanf("%d",&insert_msg.math);
                getchar();

                scanf("%d",&insert_msg.english);
                getchar();
                printf("------------input end------------\n");

                insert_data(msg_head,&insert_msg);

                printf("==========printf start==========\n");
                if(msg_head -> next != NULL)
                        printf_list(msg_head);
                printf("==========printf end=========\n");

        }

        return 0;
}

編譯運行
在這裏插入圖片描述
運行結果如下
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章