[問題] 串列鍊結

看板C_and_CPP (C/C++)作者 (fhcrc 99th ooxx)時間16年前 (2009/04/13 15:10), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/2 (看更多)
本來是想做單項的 可是刪除的地方一直做失敗 後來就改雙向的... 問題是紅色那行 我把要刪除的下一個指向要刪除的前一個就不行了 懇請高手回答 #include<stdio.h> #include<stdlib.h> #include<string.h> struct student_score{ char name[31]; int score; struct student_score *link,*front; student_score(){ char name[31]={}; score=0; } }; student_score* crate(void){ student_score *k; k = (student_score *)malloc(sizeof(student_score)); k->link=NULL; k->front=NULL; return k; } void son(student_score *next,char *r,int s){ student_score *q=crate(); next->link=q; q->front=next; q->score=s; sprintf(q->name,"%s",r); } void * delete_data(student_score *delete_now){ student_score *q,*m; q=delete_now->front; m=delete_now->link; q->link=m; m->front=q;/*這行有問題*/ free(delete_now); } struct student_score *head=crate(); int active,xx_score=0,yy_score;/*xx_score為暫存分數 b陣列暫存名子*/ int main(){ printf("(1)input information\n(2)delete the first data\n(3)print data\n"); char b[31]={}; while(scanf("%d",&active)!=EOF ){ switch(active){ case 1: printf("input name in 30 words(contain space):"); scanf("%s",b); printf("\ninput the score of the student(1~100):"); scanf("%d",&xx_score); while(xx_score>100||xx_score<0){ printf("\ninput the score of the student(1~100):"); scanf("%d",&xx_score); } if((head->score)>100||(head->score)<0){ sprintf(head->name,"%s",b); head->score=xx_score; } else{ student_score *l=head; while(l->link!=NULL){ l=l->link; } son(l,b,xx_score); } break; case 2:printf("please input the name what you want to delete:"); scanf("%s",b); student_score *l; *l=*head; char t[31]; sprintf(t,"%s",l->name); while(l->link!=NULL){ if(t==b){ l=l->link; break; } else{ l=l->link; sprintf(l->name,"%s",t); } } delete_data(l); break; case 3: student_score *replace; replace=head; while(replace!=NULL){ printf("%s\nscore:%d\n",replace->name,replace->score); replace=replace->link; } break; default :printf("please input the right number\n");break; } printf("(1)input information\n(2)delete the first data\n(3)print data\n"); } return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.60.161.254

04/13 17:00, , 1F
free掉的不是當初malloc的位址
04/13 17:00, 1F
文章代碼(AID): #19ukLZMI (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #19ukLZMI (C_and_CPP)