还差一个插入函数,插入后按学号递增打印学生信息。
自定义V+ 2019-05-23 06:33:29 #include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
struct studentNode
{
int ID;
char name[20];
struct studentNode* next;
};
struct studentNode* createlist()
{
struct studentNode* list=(struct studentNode*)malloc(sizeof(struct studentNode));
list->next=NULL;
return list;
}
struct studentNode* createnode(char *newname,int newID)
{
struct studentNode* node=(struct studentNode*)malloc(sizeof(struct studentNode));
strcpy(node->name,newname);
node->ID=newID;
node->next=NULL;
return node;
}
void insertback(struct studentNode* list,char *newname,int newID)
{
struct studentNode* newnode=createnode(newname,newID);
struct studentNode* temp=list;
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode->next=NULL;
temp->next=newnode;
}
void deleteinfo(struct studentNode* list,int ID)
{
struct studentNode* temp=list;
struct studentNode* p=list->next;
while(p->ID!=ID)
{
temp=temp->next;
p=p->next;
}
temp->next=p->next;
free(p);
}
void searchinfo(struct studentNode* list,int ID)
{
struct studentNode* temp=list->next;
while(temp->ID!=ID)
{
if(temp->next==NULL)
{
printf("没有该学生!");
return;
}
temp=temp->next;
}
printf("姓名:%s,学号:%d\n",temp->name,temp->ID);
}
void print(struct studentNode* list)
{
struct studentNode* temp=list->next;
if(temp==NULL)
{
printf("没有该学生!");
}
while(temp)
{
printf("姓名:%s,学号:%d\n",temp->name,temp->ID);
temp=temp->next;
}
}
void menu()
{
printf("**********************************\n");
printf("***********1:输入学生信息*********\n");
printf("***********2:查找学生信息*********\n");
printf("***********3:打印所有学生信息*****\n");
printf("***********4:删除学生信息*********\n");
printf("***********6:退出*****************\n");
printf("**********************************\n");
}
int choice()
{
int choice;
printf("请输入您要执行的操作:\n");
scanf("%d",&choice);
while(choice<1||choice>6)
{
printf("您的输入错误,请重新输入!");
scanf("%d",&choice);
}
return choice;
}
void work(struct studentNode* student)
{
int a=choice();
switch(a)
{
case 1:
{char name[10]="0";
int ID=0;
printf("请输入学生的姓名:\n");
scanf("%s",name);
printf("请输入学生的学号:\n");
scanf("%d",&ID);
insertback(student,name,ID);}
break;
case 2:
{int searchID=0;
printf("请输入需要查找的学生学号:\n");
scanf("%d",&searchID);
searchinfo(student,searchID);}
break;
case 3:
{print(student);}
break;
case 4:
{int deleteID=0;
printf("请输入需要删除学生的学号:\n");
scanf("%d",&deleteID);
deleteinfo(student,deleteID);}
break;
case 5:
{exit(0);}
break;
default:
printf("您的输入有误,请重新输入!\n");
break;
}
}
int main()
{
struct studentNode* stu=createlist();
while(1)
{
menu();
work(stu);
}
system("pause");
return 0;
}