关于结构体指针的问题。
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct students{
char name;
int num;
float score;
struct students *next;
};
int count=0;
int *p = &count;
void getInfo(struct students *);
void addStu(struct students **);
int delStu(struct students **,int );
int searchStu(struct students *,int );
void showList(struct students **);
int main(){
struct students *S = NULL;//生成一个头节点
int op,i;
do{
printf("welcome to students' information manage system!\n");
printf("please chose your operation:\n");
printf("1 add a student to specified location\n");
printf("2 delete a student from the specified location\n");
printf("3 find a student through his number\n");
printf("4 show the list:\n");
printf("0 exit\n");
scanf("%d",&op);
switch(op){
case 1:printf("input the student information:");addStu(&S);break;
case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
case 4:showList(&S);break;
}
}while(op != 0);
return 0;
}
void getInfo(struct students *s){
printf("input students's name :\n");
scanf("%s",s->name);
printf("input students's num and score:\n");
scanf("%d%f",&s->num,&s->score);
}
void addStu(struct students **S1){
struct students *ns,*temp,*head;
int flag = 0;
// struct students **move = S1;
ns = (struct students *)malloc(sizeof(struct students));
if(ns == NULL){
printf("menory allocation error!");
exit(1);
}
getInfo(ns);
if(*S1 == NULL){ //空链表的操作;
*S1=ns;
ns->next=NULL;
head = *S1;
}
else{
while((*S1)->num<ns->num && (*S1)->next != NULL){//通过新学生的学号来移动move与temp指针,
flag = 1;
temp = (*S1);
(*S1) = (*S1)->next;
}
if((*S1)->next == NULL && (*S1)->num<=ns->num){ //新学生的学号是最大的
(*S1)->next = ns;
ns->next = NULL;
}
else if(!flag){ //目前没有发生问题
temp = *S1;
(*S1) = ns;
ns->next = temp;
}
else{ //学生学号刚好在中间
temp->next = ns;
ns->next = (*S1);
}
}
*S1 = head; // 问题很有可能在这里
}
int delStu(struct students **S1,int n){
struct students *temp;
struct students **ptr = S1; //用来接收旧的结构体节点,并准备释放内存
if((*ptr)->next == NULL){
printf("can't delete student from an empty list\n");
}
while((*ptr)->num < n){
(*ptr)=(*ptr)->next;
}
if((*ptr) == NULL){
printf("can't find this student!\n");
}
if((*ptr)->num == n){
temp = (*ptr);
(*ptr) = temp->next;
// free(temp);
printf("delete complete\n");
}
return 0;
}
int searchStu(struct students *S1,int n){
int i = 0;
while(S1 != NULL){
if(S1->num == n){
printf("he/she is the %d student\n",i+1);
return i+1;
}
S1 = S1->next;
i++;
}
if(S1->next == NULL && S1->num != n){
printf("can't find this student");
return 0;
}
return 0;
}
void showList(struct students **S1){
struct students *s;
s = *S1;
if(*S1 == NULL){
printf("no student in this list!\n");
}
else{
printf("name\tnum\tscore\t\n");
}
while(s != NULL){
printf("%s\t%d\t%f\t\n",s->name,s->num,s->score);
s = s->next;
}
}
这个代码有很多BUG ,但是目前想弄明白是的addStu函数中的head指针的问题。
思路是用head指针获取链表的头节点的地址,在addStu函数完成后更新头结点的值,但是不知道为什么head指针有些时候里面储存的值不是头节点的值。比如你输入三个学生信息后,将S的地址传给ShowList函数后会出问题
不知道是我哪里想错了,请大佬们看看吧。
本帖最后由 cookies945 于 2019-4-6 14:06 编辑
自己偶然发现了解决的方法,欢迎大佬们也来发表一下自己的看法呀,但是还是有很多问题,比如删除链表最后一个学生,链表就没有了,选择删除操作的时候,如果输入了一个比链表长度还大的数,也会发生错误
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students{
char name;
int num;
float score;
struct students *next;
};
int count=0;
int *p = &count;
void getInfo(struct students *);
void addStu(struct students **);
int delStu(struct students **,int );
int searchStu(struct students *,int );
void showList(struct students *);
int main(){
struct students *S = NULL;//生成一个头节点
int op,i;
do{
printf("welcome to students' information manage system!\n");
printf("please chose your operation:\n");
printf("1 add a student to specified location\n");
printf("2 delete a student from the specified location\n");
printf("3 find a student through his number\n");
printf("4 show the list:\n");
printf("0 exit\n");
scanf("%d",&op);
switch(op){
case 1:printf("input the student information:");addStu(&S);break;
case 2:printf("input the num you want to delete:");scanf("%d",&i);delStu(&S,i);break;
case 3:printf("input his num:");scanf("%d",&i);searchStu(S,i);break;
case 4:showList(S);break;
}
}while(op != 0);
return 0;
}
void getInfo(struct students *s){
printf("input students's name :\n");
scanf("%s",s->name);
printf("input students's num and score:\n");
scanf("%d%f",&s->num,&s->score);
}
void addStu(struct students **S1){
struct students *ns,*temp,*move = *S1;
int flag = 0;
ns = (struct students *)malloc(sizeof(struct students));
if(ns == NULL){
printf("menory allocation error!");
exit(1);
}
getInfo(ns);
if(move == NULL){ //空链表的操作;
move=ns;
ns->next=NULL;
*S1 = move;
}
else{
while(move->num<ns->num && move->next != NULL){//通过新学生的学号来移动move与temp指针,
flag = 1;
temp = move;
move = move->next;
}
if(move->next == NULL && move->num<=ns->num){ //新学生的学号是最大的
move->next = ns;
ns->next = NULL;
}
else if(!flag){ //目前没有发生问题
temp = *S1;
move = ns;
ns->next = temp;
*S1 = move;
}
else{ //学生学号刚好在中间
temp->next = ns;
ns->next = move;
}
}
}
int delStu(struct students **S1,int n){
struct students *temp;
struct students **ptr = S1; //用来接收旧的结构体节点,并准备释放内存
if((*ptr)->next == NULL){
printf("can't delete student from an empty list\n");
}
while((*ptr)->num < n){
(*ptr)=(*ptr)->next;
}
if((*ptr) == NULL){
printf("can't find this student!\n");
}
if((*ptr)->num == n){
temp = (*ptr);
(*ptr) = temp->next;
// free(temp);
printf("delete complete\n");
}
return 0;
}
int searchStu(struct students *S1,int n){
int i = 0;
while(S1 != NULL){
if(S1->num == n){
printf("he/she is the %d student\n",i+1);
return i+1;
}
S1 = S1->next;
i++;
}
if(S1->next == NULL && S1->num != n){
printf("can't find this student");
return 0;
}
return 0;
}
void showList(struct students *S1){
struct students *s;
s = S1;
if(S1 == NULL){
printf("no student in this list!\n");
}
else{
printf("name\tnum\tscore\t\n");
}
while(s != NULL){
printf("%s\t%d\t%f\t\n",s->name,s->num,s->score);
s = s->next;
}
}
页:
[1]