#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student
{
char name[20];
char id[20];
struct Student* next;
}student;
//创建链表
student* Create(){
student* head = (student*)malloc(sizeof(student));//头指针
student *end,*new;//尾节点,新节点
end = new = (student*)malloc(sizeof(student));
printf("请输入姓名,学号:");
setbuf(stdin,NULL);
scanf("%s %s",new->name,new->id);
while(1){
end = new;
end->next = NULL;
head->next = end;
new = (student*)malloc(sizeof(student));
printf("是否继续(Y/N)");
setbuf(stdin,NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
printf("请输入下一个同学的姓名,学号:");
setbuf(stdin,NULL);
scanf("%s %s",new->name,new->id);
}
free(new);
return head;
}
//头插
student* headNode(student* head,char name[],char id[]){
student* newNode = (student*)malloc(sizeof(student));
strcpy(newNode->name,&name);
strcpy(newNode->id,&id);
newNode->next = head->next;
head->next = newNode;
return head;
}
//删除
// student* DeleteNode(student* head, ){
// }
//遍历链表
void printLink(student* head){
student* p = head->next;
while (p != NULL)
{
printf("姓名:%s,学号:%s",p->name,p->id);
printf("\n");
p = p->next;
}
}
int main()
{
student* link = Create();
printLink(link);
headNode(link,'ba','002');
printf("\n插入之后为:");
printLink(link);
return 0;
}

请输入姓名,学号: aa 001
是否继续(Y/N)n
姓名:aa,学号:001
请插入:姓名:ab,学号:200
姓名: aa,学号: 001
PS D:lvsc\dmlprojects Whelloworld>