#include<stdio.h>
#include<stdlib.h>
struct student{
char name[20];
int num;
int score;
};
struct Node {
struct student data;
struct Node * next;
};
struct Node * creatlist(){
struct Node * headnode=(struct Node*)malloc(sizeof(struct Node));
headnode->next=NULL;
return headnode;
}
struct Node creatnode(struct student data){
struct Nodenewnode=(struct Node*)malloc(sizeof(struct Node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
//打印
void printlist(struct Node* headnode)
{
struct Node * pmove;
printf(“name\tnum\tscore\n”);
pmove=headnode->next;
while(pmove)
{
printf("%s\t%d\t%d\n",pmove->data.name,pmove->data.num,pmove->data.score);
pmove=pmove->next;
}
printf("\n");
}
//头部插入
void inserthead(struct Node* headnode,struct student data)
{
struct Node *newnode=creatnode( data);
newnode->next=headnode->next;
headnode->next=newnode;
}
//尾部插入
void SListPushBack(struct Node* headnode,struct student data)
{
struct Node node=creatnode( data);
if (headnode== NULL)
{
node->next = headnode;
headnode = node;
return;
}
Node cur = headnode;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = node;
node->next = NULL;
}
//删除
void deletedata(struct Nodeheadnode,int num)
{
struct Nodepastnode=headnode->next;
struct Node*pastfront=headnode;
if(pastnode==NULL)
printf(“链表为空,无法删除\n”);
while(pastnode->data.num!=num){
pastfront=pastnode;
pastnode=pastfront->next;
if(pastnode==NULL)
{
printf("未找到指定位置\n");
return;
}
}
pastfront->next=pastnode->next;
free(pastnode) ;
}
int main(){
struct Node*list=creatlist();
struct student info;
while(1){
printf(“请输入学生的姓名,编号,成绩:”);
setbuf(stdin,NULL);
scanf("%s %d %d",info.name,&info.num,&info.score);
//inserthead(list,info);
SListPushBack(list,info);
printf(“continue y/n?\n”);
setbuf(stdin,NULL);
int choice=getchar();
if(choice==‘n’||choice==‘N’){
break;
}
}
printlist(list);
printf(“请输入要删除学生的编号:”);
scanf("%d",&info.num);
deletedata(list,info.num);
printlist(list);
system(“pause”);
return 0;
}