用c++单链表写出学生管理系统
时间: 2023-03-09 21:24:29 浏览: 136
学生管理系统的c语言单链表实现可以建立如下:定义结构体:struct Student{
char name[20]; //姓名
int age; //年龄
char sex[4]; //性别
struct Student *next; //指向下一个学生的指针
};创建链表:struct Student *CreateLink(){
struct Student *head;
struct Student *p1,*p2;
int n=0;
p1=p2=(struct Student *)malloc(sizeof(struct Student));
scanf("%s %d %s",p1->name,&p1->age,p1->sex);
p1->next=NULL;
head=NULL;
while(p1->name[0]!='0'){
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Student *)malloc(sizeof(struct Student));
scanf("%s %d %s",p1->name,&p1->age,p1->sex);
p1->next=NULL;
}
return(head);
}插入节点:struct Student *InsertNode(struct Student *head,struct Student *stud){
struct Student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL){
head=p0;
p0->next=NULL;
}
else{
while((p0->age > p1->age) && (p1->next!=NULL)){
p2=p1;
p1=p1->next;
}
if(p0->age <= p1->age){
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else{
p1->next=p0;
p0->next=NULL;
}
}
return(head);
}查找节点:struct Student *FindNode(struct Student *head,char *name){
struct Student *p;
p=head;
if(head==NULL) return(NULL);
else{
while(strcmp(name,p->name)!=0){
p=p->next;
if(p==NULL) return(NULL);
}
return(p);
}
}删除节点:struct Student *DeleteNode(struct Student *head,char *name){
struct Student *p1,*p2;
if(head==NULL) return(head);
p1=head;
while(strcmp(name,p1->name)!=0){
p2=p1;
p1=p1->next;
if(p1==NULL) return(head);
}
if(p1==head) head=p1->next;
else p2->next=p1->next;
free(p1);
return(head);
}
阅读全文