当前位置: 代码迷 >> C语言 >> 链表运用出的问题
  详细解决方案

链表运用出的问题

热度:195   发布时间:2005-10-20 12:22:00.0
链表运用出的问题

 我写了程序,老师要求用链表。结果出了个问题怎么也改不了。提示说INSERT函数有问题。大家帮忙看看?

#define NULL0 #define LEN sizeof (struct student) #include <stdio.h> struct student {long num; char name [20]; char sex; int age; float score [10] ; struct students*next; }; int n ,i, score [10]; struct student*create() {struct student *head,*p1,*p2; n=0; head=NULL; p1=(struct student *)malloc(LEN); scanf("%d%c%c%d",&p1->num,p1->name[20],&p1->sex,&p1->age); for(i=0;i<10;i++) {scanf("%d",&score[i]);}; p1->next=NULL; while(p1->num!=0) {++n; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); scanf("%d%c%c%d",&p1->num,p1->name[20],&p1->sex,&p1->age); for(i=0;i<10;i++) scanf("%d",&score[i]); p1->next=NULL; }free(p1); return(head); } struct student *insert(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->num>p1->num)&&(p1!=NULL)) {p2=p1;p1=p1->next;} if(head==p1) {head=p0; p0->next=head;} else

{p2->next=p0; p0->next=p1; } ++n; return (head); } struct student *delete(struct student *head,long num) {struct student *p1,*p2; if(head==NULL) goto end; else p1=head; while((num!=p1->num)&&(p1->next!=NULL)) {p2=p1;p1=p1->next;} if(num==p1->num) {if(p1==head) head=p1->next; else p2->next=p1->next; printf("delete:%d\n",num); free(p1); n--; } else printf("%d not been found!\n",num); end:return(head); } struct student *find(struct student *head,long num) {struct student p1,*p2; int i; if(head==NULL) {printf("\n list NULL!\n");goto end;} p1=head; while((num!=p1->num)&&(p1!=NULL)) {p2=p1;p1=p1->next;} if(p1!=NULL) {printf("find:%d%c%c%d",num,name,sex,age); for(i=0;i<10;i++) printf("%d",score[i]); } else printf("%d not been found!\n",num); end: return(head); } float aver(float score[]) {int i; for(i=0;i<10;i++) aver+=score[i]; aver=aver/10; return(aver); } float highest(float score[]) {int i; highest=score[0]; for(i=1;i<10;i++) {if(score[i]>highest) highest=score;} return(highest); } float lowest(float score[]) {int i; lowest=score[0]; for(i=1;i<10;i++) {if(lowest>score[i]) lowest=score[i]; } return lowest; } main() {char a; printf("Welcome to students' MIS!\n"); scanf("%c",&a); switch(a) {case I:struct student*insert(struct student*head,struct student*stud);break; case D:struct student*delete(struct student*head,long num);break; case F:struct student*find(struct student*head,long num);break; case C:float aver(float score[])}; float highest(float score[]); float lowest(float score[]);break; case Q:printf("\n"); } }

搜索更多相关的解决方案: 链表  

----------------解决方案--------------------------------------------------------
插入的时候要先分配空间 然后在插入
----------------解决方案--------------------------------------------------------
那具体应该怎么分配呢?
----------------解决方案--------------------------------------------------------
应该在插入之前给p2分配内存空间!
----------------解决方案--------------------------------------------------------
你的程序写得很乱啊,#define NULL0 #define LEN sizeof (struct student) #include <stdio.h> struct student {long num; char name [20]; char sex; int age; float score [10] ; struct students*next; };这是最明显得一个错误
----------------解决方案--------------------------------------------------------

#include<malloc.h> #define NULL 0 #define LEN sizeof(struct student) struct student {int num; int score; struct student *next; }; int n; struct student *creat(void) {struct student *head; struct student *p1,*p2; n=0; p1=p2=(struct student*)malloc(LEN); scanf("%d%d",&p1->num,&p1->score); head=NULL; while(p1->num!=0) {n=n+1; if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); scanf("%d%d",&p1->num,&p1->score); } p2->next=NULL; return(head); }

void print(struct student *head) {struct student *p; printf("\nNow,these %d record are :\n",n); p=head; if(head!=NULL) do {printf("%d%d\n",p->num,p->score); p=p->next; }while(p!=NULL); }

struct student *del(struct student *head,int num) {struct student *p1,*p2; if(head==NULL) {printf("\nlist null! \n"); } p1=head; while(num!=p1->num&&p1->next!=NULL) {p2=p1; p1=p1->next;} if(num==p1->num) {if(p1==head) head=p1->next; else p2->next=p1->next; printf("delete :%d\n",num); n=n-1;} else printf("%d not been found!\n",num); return(head);}

struct student *insert(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->num>p1->num)&&(p1->next!=NULL)) {p2=p1;p1=p1->next;} if(p0->num<=p1->num) {if(head==p1) head=p0; else p2->next=p0; p0->next=p1; } else { p1->next=p0; p0->next=NULL; } }

return(head); }

main() { struct student *head,stu; long del_num; printf("input records:\n"); head=creat(); print(head); printf("\ninput the deleted number:\n"); scanf("%d",&del_num); head=del(head,del_num); print(head); printf("\ninput inserted record:\n"); scanf("%d%d",&stu.num,&stu.score); head=insert(head,&stu); print(head); getch(); }


----------------解决方案--------------------------------------------------------
告诉我最后的getch()做什么用,我对这个函数基本没什么认识,究竟是从哪get,get什么,存倒哪里?对整个程序有什么影响?我看到好几个程序最后都用到这个函数,所以请能将详细的人好好的说一下,至于“取得一个字符,但不输出”之类的答案就免了。getchar()一样不输出,但是用getchar()的时候会把取得的字符赋值给某个变量,这里的getch到底是做什么用的呢?
----------------解决方案--------------------------------------------------------
  相关解决方案