我是改改,帮我改改程序

帮我改改程序 - 故障解答 - 电脑教程网

帮我改改程序

日期:2006-12-21   荐:
帮我改改程序写了个链表的程序,编译通过,运行有错,改了很久就是不对,大虾帮我:#include<stdlib.h>#include<malloc.h>#include<stdio.h>#include<string.h>#define OK 1#define ERROR 0struct STU{ char name[10]; int stuno; int age; int score;};typedef struct STU ElemType;struct LNODE{ ElemType date; struct LNODE *next;};typedef struct LNODE lnode;int init(lnode *L){ L=(lnode *)malloc(sizeof(lnode)); L->next=NULL; return OK;}int listlength(lnode *L){ int i=1; while(L->next) { L=L->next; i ; } return i;}int getelem(lnode *L,int i,ElemType *e){ int j; for(j=1;j<i;j ) { L=L->next; } *e=L->date;}int equallist(ElemType *A,ElemType *B){ if(strcmp(A->name,B->name)==0) return 1; else return 0;}int less_equallist(ElemType *A,ElemType *B){ if(strcmp(A->name,B->name)<=0) return 1; else return 0; }int locateElem(lnode *L,ElemType e){ int i; for(i=1;i<listlength(L);i ) { if(equallist(&L->date,&e)) { return i; break; } } return 0;}int mergelist(lnode *La,lnode *Lb,lnode *Lc){ lnode *pa,*pb,*pc; pa=La->next; pb=Lb->next; while(pa&&pb) { if(less_equallist(&pa->date,&pb->date)) { pc=pa;pa=pa->next; } else { pc=pb;pb=pb->next; } pc->next=pa?pa:pb; } return OK;}int listinsert(lnode *L,int i,ElemType e){ lnode *p; int j; for(j=1;j<i;j ) { L=L->next; } if(!L) { return ERROR; } p=(lnode *)malloc(sizeof(lnode)); p->date=e; p->next=L->next; L->next=p; return OK;} int inputdate(ElemType a){ printf("please input the name "); scanf("%s",a.name); printf("please input the stuno "); scanf("%d",a.stuno); printf("please input the age "); scanf("%d",a.age); printf("please input the score "); scanf("%d",a.score); return OK;}int printlist(lnode L){ int i; lnode *p; p=&L; printf("name stuno age score"); while(p->next) { p=p->next; printf("%-10s %d\t%d\t%d", p->date.name, p->date.stuno, p->date.age, p->date.score); } printf("");}main(){ lnode L; ElemType e; init(&L); inputdate(e); listinsert(&L,1,e); printlist(L); system("PAUSE");}呵呵什么地方?#include<stdlib.h>#include<stdio.h>#include<string.h>#define OK 1#define ERROR 0typedef struct STU{ char name[10]; int stuno; int age; int score;}ElemType;;struct LNODE{ ElemType date; struct LNODE *next;};typedef struct LNODE lnode;int init(lnode *L){ L=(lnode *)malloc(sizeof(lnode)); L->next=NULL; return OK;}int listlength(lnode *L){ int i=1; while(L->next) { L=L->next; i ; } return i;}int getelem(lnode *L,int i,ElemType *e){ int j; for(j=1;j<i;j ) { L=L->next; } *e=L->date;}int equallist(ElemType *A,ElemType *B){ if(strcmp(A->name,B->name)==0) return 1; else return 0;}int less_equallist(ElemType *A,ElemType *B){ if(strcmp(A->name,B->name)<=0) return 1; else return 0;}int locateElem(lnode *L,ElemType e){ int i; for(i=1;i<listlength(L);i ) { if(equallist(&L->date,&e)) { return i;/* break; */ } } return 0;}int mergelist(lnode *La,lnode *Lb,lnode *Lc){ lnode *pa,*pb,*pc; pa=La->next; pb=Lb->next; pc=Lc->next; while(pa&&pb) { if(less_equallist(&pa->date,&pb->date)) { pc=pa;pa=pa->next; } else { pc=pb;pb=pb->next; } pc->next=pa?pa:pb; } return OK;}int listinsert(lnode *L,int i,ElemType e){ lnode *p; int j; for(j=1;j<i;j ) { L=L->next; } if(!L) { return ERROR; } p=(lnode *)malloc(sizeof(lnode)); p->date=e; p->next=L->next; L->next=p; return OK;}int inputdate(ElemType *a){ a = (struct STU *) malloc(sizeof(struct STU)); printf("please input the name "); scanf("%s",a->name); printf("please input the stuno "); scanf("%d",a->stuno); printf("please input the age "); scanf("%d",a->age); printf("please input the score "); scanf("%d",a->score); return OK;}int printlist(lnode L){ int i; lnode *p; p=&L; printf("name stuno age score"); while(p->next) { printf("%s %d\t%d\t%d", p->date.name, p->date.stuno, p->date.age, p->date.score); p = p->next; } printf("");}main(){ lnode L;/* ElemType e; */ struct STU a1; init(&L);/* a1 =(struct STU) malloc(sizeof(struct STU)); */inputdate(&a1); listinsert(&L,1,a1); printlist(L); system("PAUSE");}可以运行了,但输出的地方还有点错你仔细调试一下还是不行啊在输入了stuno后就出错了啊你要输入整形数呀,我的编译环境是tc 2.0我有个以前自己写的简单链表程序,你参考一下吧:#include <iostream.h>void main(){int i,m;struct name{int num;name *ptr;} *Head,*q,*p;Head=NULL;q=NULL;p=new name;p->ptr=NULL;Head=p;q=p;p->num=1;cout<<"Please input a number which means the length of the chain"<<endl;cin>>m;for(i=2;i<=m;i ){p=new name;p->num=i;q->ptr=p;q=p;}p->ptr=NULL;p=Head;q=Head;while(q->ptr!=NULL){p=q->ptr;cout<<q->num<<endl;q=p;}cout<<q->num<<endl;}哪位大虾帮我再看看哦太长了,你还是跟踪调试吧,看问题出在哪个函数.inputdate函数应该改成这样:int inputdate(ElemType a){ printf("please input the name "); scanf("%s",a.name); printf("please input the stuno "); scanf("%d",&a.stuno); ////////////////////////加& printf("please input the age "); scanf("%d",&a.age); printf("please input the score "); scanf("%d",&a.score); return OK;}#include<stdlib.h>#include<malloc.h>#include<stdio.h>#include<string.h>#define OK 1#define ERROR 0struct STU{ char name[10]; int stuno; int age; int score;};typedef struct STU ElemType;//你这里定义的有问题了,ElemType是一个字符类型把它给成其他的然后typedef ElemType int或者char或其他的在头文件上定义就可struct LNODE{ ElemType date; struct LNODE *next;};typedef struct LNODE lnode;int init(lnode *L){ L=(lnode *)malloc(sizeof(lnode)); L->next=NULL; return OK;}int listlength(lnode *L){ int i=1; while(L->next) { L=L->next; i ; } return i;}int getelem(lnode *L,int i,ElemType &e){ int j; for(j=1;j<i;j ) { L=L->next; } e=L->date;}int equallist(ElemType *A,ElemType *B){ if(strcmp(A->name,B->name)==0) return 1; else return 0;}int less_equallist(ElemType *A,ElemType *B){ if(strcmp(A->name,B->name)<=0) return 1; else return 0; }int locateElem(lnode &L,ElemType &e){ int i; for(i=1;i<listlength(L);i ) { if(equallist(L->date,e)) { return i; } } return 0;}int mergelist(lnode *La,lnode *Lb,lnode *Lc){ lnode *pa,*pb,*pc; pa=La->next; pb=Lb->next; while(pa&&pb) { if(less_equallist(pa->date,pb->date)) { pc=pa;pa=pa->next; } else { pc=pb;pb=pb->next; } pc->next=pa?pa:pb; } return OK;}int listinsert(lnode *L,int i,ElemType e){ lnode *p; int j; for(j=1;j<i;j ) { L=L->next; } if(!L) { return ERROR; } p=(lnode *)malloc(sizeof(lnode)); p->date=e; p->next=L->next; L->next=p; return OK;} int inputdate(ElemType a){ printf("please input the name "); scanf("%s",a.name); printf("please input the stuno "); scanf("%d",a.stuno); printf("please input the age "); scanf("%d",a.age); printf("please input the score "); scanf("%d",a.score); return OK;}int printlist(lnode L){ int i; lnode *p; p=&L; printf("name stuno age score"); while(p->next) { p=p->next; printf("%-10s %d\t%d\t%d", p->date.name, p->date.stuno, p->date.age, p->date.score); } printf("");}main(){ lnode L; ElemType e; init(&L); inputdate(e); listinsert(&L,1,e); printlist(L); system("PAUSE");}如何跟踪调试???那个好心人请帮忙发个帖子(急),我这里发不了帖子啊,谢谢了,:编写一个程序,要求局域网内运行了该程序的所有机器时间同步(精确度要求不高)。我的qq:4067756,有会的加qq也好?gt;>?hr>init ()不好,应该是init ( lnode *L ){ L = NULl;}空list,不用分配空间init函数是有问题的,你传的是一个指针参数,函数会将该指针复制一份,你在函数中又将指针指向了一个新分配的地方,但原来的指针指向是没有变的,典型的指针悬空了。如何跟踪调试???//按F7键说不定是你的链表操作那里有错也 最好在草稿上按你程序把结构图画出来看看
标签: