求大佬看看这个程序的哪里想法有问题吧
#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 *);
int addStu(struct students **,int );
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 num you want to insert:");scanf("%d",&i);addStu(&S,i);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);
}
int addStu(struct students **S1,int n){
struct students *ns,*temp;
int i;
if(n < 0 || n > count+1){
printf("error:invalid num");
return 1;
}
ns = (struct students *)malloc(sizeof(struct students));
if(ns == NULL){
printf("menory allocation error!");
}
getInfo(ns);
for(i = 0;i < n-1;i++){
*S1 = (*S1)->next;
}
if(*S1 !=NULL){
temp = *S1;
*S1 = ns;
ns->next = temp;
}
else{
*S1 = ns;
ns->next = NULL;
}
(*p)++;
return 0;
}
int delStu(struct students **S1,int n){
struct students *temp; //用来接收旧的结构体节点,并准备释放内存
int i;
if(n > count || n < 0){
printf("error!");
return 1;
}
for(i = 0;i < n-1;i++){
*S1 = (*S1)->next;
}
temp = *S1;
*S1 = temp->next;
free(temp);
printf("delete complete");
(*p)--;
return 0;
}
int searchStu(struct students *S1,int n){
int i;
for(i = 0;i < count;i++){
if(S1->num == n){
printf("he/she is the %d student");
return i+1;
}
S1 = S1->next;
}
if(i > count){
printf("can't find this student");
return 0;
}
}
void showList(struct students *S1){
int i;
struct students *s;
s = S1;
if((*p) == 0){
printf("no student in LinkList");
}
while(s != NULL){
printf("name\tnum\tscore\n");
printf("%s\t%d\t%f\n",s->name,s->num,s->score);
s = s->next;
}
}
插到第几个,第几个前面的就没有了,插到最后一个整个链表都没了{:10_266:}
救救孩子吧
想法很好 {:10_257:} 找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students {
char name ;
int num ;
float score ;
struct students * next ;
} ;
int count = 0 ;
struct students * findStu(struct students * hd , const int num)
{
struct students * p1 , * p2 , * ret ;
bool f ;
ret = NULL ;
if(count > 0 && hd != NULL) {
for(f = false , p1 = p2 = hd ; p2 != NULL && ! f ;) {
if(p2 -> num == num) {
f = true ;
} else {
p1 = p2 ;
p2 = p1 -> next ;
}
}
if(f) ret = p1 ;
}
return ret ;
}
struct students * addStu(struct students * hd)
{
struct students * p1 , * p2 , * head ;
int num ;
head = hd ;
p1 = hd ;
if(p1 != NULL) while(p1 -> next != NULL) p1 = p1 -> next ;
for(;;) {
printf("\n") ;
printf("input student\'s num to add : ") ;
scanf("%d" , & num) ;
if(! num) break ;
if(findStu(head , num) == NULL) {
if((p2 = (struct students *) malloc(sizeof(struct students))) != NULL) {
printf("input student\'s name : ") ;
scanf("%s" , p2 -> name) ;
printf("input student\'s score: ") ;
scanf("%f" , & p2 -> score) ;
p2 -> num = num ;
p2 -> next = NULL ;
if(! count) head = p2 ;
else p1 -> next = p2 ;
p1 = p2 ;
count ++ ;
} else {
fprintf(stderr , "malloc() cann\'tto alocate the memory !") ;
break ;
}
} else {
printf("\n") ;
printf("the student with num = %d already exists !\n" , num) ;
}
}
return head ;
}
struct students * delStu(struct students * hd)
{
struct students * p1 , * p2 , * head ;
int num ;
head = hd ;
if(count > 0 && hd != NULL) {
while(count > 0) {
printf("\n") ;
printf("input student\'s num to delete : ") ;
scanf("%d" , & num) ;
if(! num) break ;
if((p1 = findStu(head , num)) != NULL) {
if(p1 == head) {
head = head -> next ;
p2 = p1 ;
} else {
p2 = p1 -> next ;
p1 -> next = p2 -> next ;
}
free(p2) ;
count -- ;
printf("the student with num = %d has been successfully deteted !\n" , num) ;
} else {
printf("\n") ;
printf("the student with num = %d doesn\'t exists !\n" , num) ;
}
}
} else {
printf("\n") ;
printf("empty list !\n") ;
}
return head ;
}
void searchStu(struct students * hd)
{
struct students * p ;
int num ;
if(count > 0 && hd != NULL) {
for(;;) {
printf("\n") ;
printf("input student\'s num to search : ") ;
scanf("%d" , & num) ;
if(! num) break ;
if((p = findStu(hd , num)) != NULL) {
printf("p -> num = %d\n" , p -> num) ;
if(p != hd) p = p -> next ;
printf("student's name: %s\n" , p -> name) ;
printf("student's num : %d\n" , p -> num) ;
printf("student's score : %5.2f\n" , p -> score) ;
} else {
printf("\n") ;
printf("the student with num = %d doesn\'t exists !\n" , num) ;
}
}
} else {
printf("\n") ;
printf("empty list !\n") ;
}
}
void showList(struct students * hd)
{
struct students * p ;
if(count > 0 && hd != NULL) {
p = hd ;
while(p != NULL) {
printf("\n") ;
printf("student\'s name: %s\n" , p -> name);
printf("student\'s num : %d\n" , p -> num) ;
printf("student\'s score : %5.2f\n" , p -> score) ;
p = p -> next ;
}
} else {
printf("\n") ;
printf("empty list !\n") ;
}
}
int main()
{
struct students * head ;
int op ;
head = NULL ;
count = 0 ;
do {
printf("\n") ;
printf("welcome to students\' information manage system!\n") ;
printf(" 1 add a student.\n") ;
printf(" 2 delete a student.\n") ;
printf(" 3 find a student.\n") ;
printf(" 4 show the list:\n") ;
printf(" 0 exit\n") ;
printf("\n") ;
printf(" your choice : ") ;
scanf("%d" , & op) ;
switch(op) {
case 1 :
head = addStu(head) ;
break ;
case 2:
head = delStu(head) ;
break ;
case 3:
searchStu(head) ;
break ;
case 4:
showList(head) ;
break ;
}
} while(op != 0) ;
}
jackz007 发表于 2019-3-24 20:17
找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。
我在网上又看到和你这个差不多的,就是看不懂才来论坛问的{:10_266:} 本帖最后由 jackz007 于 2019-3-25 18:09 编辑
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普通变量变来变去,而这个变量 S1 又是通过地址传入,在函数中所有的改变都会映射到主函数中去,而在主函数中,却一直认为这个 S1 是链表首节点指针,从而造成严重混乱。
我贴出来的代码是这次才精心编写的,没有照抄任何人的东西,你前面看见相似的代码估计也是我给别人的回帖。 jackz007 发表于 2019-3-25 17:56
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普 ...
哦 原来如此,难怪我看很多人写的都有另外的指针来替代头节点
谢谢大佬,受教了
jackz007 发表于 2019-3-25 17:56
addStu() 的最大问题是 S1 作为指向链表首节点的指针,其重要性不言而喻,可是在函数中却被当做普 ...
大佬,这个程序还是不行啊,就算改用其他指针指向头节点,也不能写道这个头节点里啊#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 *);
int 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 num you want to insert:");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);
}
int addStu(struct students **S1){
struct students *ns,*temp,*ptr = *S1;
ns = (struct students *)malloc(sizeof(struct students));
if(ns == NULL){
printf("menory allocation error!");
}
getInfo(ns);
if(ptr == NULL){
ptr = ns;
ns->next = NULL;
(*p)++;
}
while(ptr->next != NULL && ptr->num < ns->num){
temp = ptr;
ptr = ptr->next;
}
if(ptr == *S1){
temp = *S1;
*S1 = ns;
ns->next = temp;
}
else if(ptr->next == NULL){
ptr->next = ns;
ns->next = NULL;
}
else{
temp->next = ns;
ns->next = ptr;
}
printf("name\tnum\tscore\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
return 0;
}
int delStu(struct students **S1,int n){
struct students *temp,*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;
while(S1 != NULL){
if(S1->num == n){
printf("he/she is the %d student");
return i+1;
}
S1 = S1->next;
}
if(S1->next == NULL && S1->num != n){
printf("can't find this student");
return 0;
}
}
void showList(struct students **S1){
if(*S1 == NULL){
printf("no student in this list!\n");
}
else{
printf("name\tnum\tscore\t\n");
}
while(*S1 != NULL){
printf("%s\t%d\t%d\t\n",(*S1)->name,(*S1)->num,(*S1)->score);
*S1 = (*S1)->next;
}
}
页:
[1]