| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 597 人关注过本帖
标题:链表运用出的问题
只看楼主 加入收藏
hbqcgyxyjsj
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-10-20
收藏
 问题点数:0 回复次数:6 
链表运用出的问题

 我写了程序,老师要求用链表。结果出了个问题怎么也改不了。提示说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"); } }

搜索更多相关主题的帖子: 链表 
2005-10-20 12:22
wenyong
Rank: 1
等 级:新手上路
帖 子:251
专家分:0
注 册:2005-8-9
收藏
得分:0 
插入的时候要先分配空间 然后在插入

2005-10-20 12:25
hbqcgyxyjsj
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-10-20
收藏
得分:0 
那具体应该怎么分配呢?
2005-10-20 22:59
kaituozhe
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-10-20
收藏
得分:0 
应该在插入之前给p2分配内存空间!
2005-10-21 06:33
猪也聪明
Rank: 1
等 级:新手上路
帖 子:156
专家分:0
注 册:2005-5-16
收藏
得分:0 
你的程序写得很乱啊,#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; };这是最明显得一个错误

虽然我没有翅膀,可是我希望飞的高点
2005-10-21 13:33
firstlijia
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-10-8
收藏
得分:0 

#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(); }


2005-10-21 15:16
socks
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2005-10-13
收藏
得分:0 
告诉我最后的getch()做什么用,我对这个函数基本没什么认识,究竟是从哪get,get什么,存倒哪里?对整个程序有什么影响?我看到好几个程序最后都用到这个函数,所以请能将详细的人好好的说一下,至于“取得一个字符,但不输出”之类的答案就免了。getchar()一样不输出,但是用getchar()的时候会把取得的字符赋值给某个变量,这里的getch到底是做什么用的呢?

虔诚的初学者~~~
2005-10-21 17:44
快速回复:链表运用出的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018217 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved