#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#define ERR 1
#define OK 0
typedef struct stu_info_t STU_INFO_T;
extern STU_INFO_T *linklist_init();
extern STU_INFO_T *stu_info_add(int n, STU_INFO_T *p_head);
typedef struct stu_info_t
{
unsigned int age;
char name[50];
float grade;
char stu_class[50];
struct stu_info_t *p_next;
}STU_INFO_T;
STU_INFO_T *linklist_init()
{
STU_INFO_T *p_head = NULL;
STU_INFO_T *p_cur = NULL;
p_head = (STU_INFO_T *)malloc(sizeof(STU_INFO_T));
if (p_head == NULL)
{
printf("<%s:%d>memory request error!\n", __FUNCTION__, __LINE__);
exit(ERR);
}
p_cur = p_head;
p_cur->p_next = NULL;
return p_head;
}
STU_INFO_T *stu_info_add(int n, STU_INFO_T *p_head)
{
int i = 0;
STU_INFO_T *p_tmp = NULL;
STU_INFO_T *p_cur = NULL;
char name[50] = {0};
int age = 0;
char stu_class[50] = {0};
float grade = 0;
p_cur = p_head;
for(i = 0; i < n; i++)
{
p_tmp = (STU_INFO_T *)malloc(sizeof(STU_INFO_T));
if (p_tmp == NULL)
{
printf("<%s:%d>memory request error!\n", __FUNCTION__, __LINE__);
exit(ERR);
}
memset(name, 0, sizeof(name));
printf("please input the student[%d] name:\n", i);
scanf("%s", name);
printf("please input the student[%d] age:\n", i);
scanf("%d", &age);
printf("please input the student[%d] class:\n", i);
scanf("%s", stu_class);
printf("please input the student[%d] grade:\n", i);
scanf("%f", &grade);
memcpy(p_tmp->name, name, 50);
memcpy(p_tmp->stu_class, stu_class, 50);
p_tmp->age = age;
p_tmp->grade = grade;
p_tmp->p_next = NULL;
p_cur->p_next = p_tmp;
p_cur = p_tmp;
}
return p_head;
}
int stu_info_prt(STU_INFO_T *p_head)
{
STU_INFO_T *p_tmp = NULL;
system("CLS");
printf("\
name\
age\
class\
grade\n");
p_tmp= p_head;
p_tmp = p_tmp->p_next;
if (NULL == p_tmp)
{
printf("linklist is empty!\n");
return OK;
}
while (NULL != p_tmp)
{
printf("%14s %12d %15s %10.2f\n",
p_tmp->name,
p_tmp->age,
p_tmp->stu_class,
p_tmp->grade);
p_tmp = p_tmp->p_next;
}
return OK;
}
int main()
{
int sel = 0;
int n = 0;
STU_INFO_T *p_head = NULL;
p_head = linklist_init();
while (1)
{
printf("\n***************************************************\n");
printf("***************************************************\n");
printf("******** ********\n");
printf("******** ********\n");
printf("******** stu info manag sys ********\n");
printf("******** ********\n");
printf("******** 1.info_dispaly ********\n");
printf("******** 2.info_add ********\n");
printf("******** 3.info_del ********\n");
printf("******** 4.info_save ********\n");
printf("******** 5.exit ********\n");
printf("******** ********\n");
printf("***************************************************\n");
printf("***************************************************\n");
printf("please make your choice:\n");
scanf("%d", &sel);
switch(sel)
{
case 1:
{
(void)stu_info_prt(p_head);
break;
}
case 2:
{
printf("please input the number of students:\n");
scanf("%d", &n);
(void)stu_info_add(n, p_head);
break;
}
case 5:
{
exit(0);
}
default:
{
break;
}
}
}
return OK;
}
C语言链表
最新推荐文章于 2024-02-27 17:53:45 发布