功能
简易版
1,增加,
2,删除
3,修改
4,查看,
5,退出,
而且只有学生姓名和电话号码。
注意我修改学生信息是用的strncpy()函数,而且因为是电话号码,所以我定义的替换前11个字符。
而且我的vim没有中文输入,所以用的中文版英语,哈哈。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[20];
char number[20];
struct student *next;
};
void putstudent(struct student *new)
{
printf("Please input the add student name:");
scanf("%s",new->name);
printf("Please input the add student number:");
scanf("%s",new->number);
}
void add_student(struct student **head)
{
struct student *new;
static struct student *tail;
new = (struct student*)malloc(sizeof(struct student));
if(new == NULL)
{
printf("space is low");
exit(1);
}
putstudent(new);
if(*head != NULL)
{
tail->next = new;
new->next = NULL;
}
else
{
*head = new;
new->next = NULL;
}
tail = new;
}
void delete_student(struct student **head)
{
char delete[20];
struct student *current;
struct student *previous;
printf("Please input the delete student name:");
scanf("%s",delete);
current = *head;
previous = NULL;
while(current != NULL && strcmp(current->name, delete))
{
previous = current;
current = current->next;
}
if(current == NULL)
{
printf("Don't find the student\n");
}
else
{
if(previous == NULL)
{
*head = current->next;
}
else
{
previous->next = current->next;
}
free(current);
}
}
void revise_student(struct student **head)
{
char name[20],num[20];
struct student *current;
struct student *previous;
printf("Please input the revise student name:");
scanf("%s",name);
current = *head;
previous = NULL;
while(current != NULL && strcmp(current->name, name))
{
previous = current;
current = current->next;
}
if(current == NULL)
{
printf("Don't find the student\n");
}
else
{
printf("name:%s ",current->name);
printf("number:%s\n",current->number);
printf("please input the revise number:");
scanf("%s",num);
strncpy(current->number,num,11);
}
}
void view_student(struct student *head)
{
printf("--current student system-----\n");
while(head != NULL)
{
printf("name:%s ",head->name);
printf("number:%s\n",head->number);
head = head->next;
}
}
int main(void)
{
int num;
struct student *head = NULL;
printf("------------------------------------\n");
printf("-----------student system-----------\n");
printf("--1,Please input the add student----\n");
printf("--2,Please input the delete student-\n");
printf("--3,Please input the revise student-\n");
printf("--4,View current student system-----\n");
printf("--5,Exit student system-------------\n");
printf("------------------------------------\n");
while(1)
{
printf("please input number:");
scanf("%d",&num);
switch(num)
{
case 1 : add_student(&head); break;
case 2 : delete_student(&head); break;
case 3 : revise_student(&head); break;
case 4 : view_student(head); break;
case 5 : return 0;
}
}
}