首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于数据结构中链表的有关问题,请帮忙找异常!

2012-07-22 
关于数据结构中链表的问题,请帮忙找错误!![codeC/C++][/code]#include stdafx.h#include stdio.h#inc

关于数据结构中链表的问题,请帮忙找错误!!
[code=C/C++][/code]#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct LNode{ // 项的表示,多项式的项作为LinkList的数据元素
int coef;//系数
int expn;//指数
struct LNode *next;
}*LinkList;

void InitLinkList(LinkList &L){
//建立链表的头结点
L = (LNode*)malloc(sizeof(LNode));
if(!L)
printf("Error");
L->next = NULL;
}

void CreateLinkList(LinkList &L){
int coef;
int expn;
LinkList q,p;
q = L;
printf("请输入该项的系数:\n");
scanf("%d",&coef);
//printf("注意:您输入的系数为:%d\n",coef);
printf("请输入该项的幂:\n");
scanf("%d",&expn);
//printf("注意:您输入的幂为:%d\n",expn);
while(coef != 0 || expn != 0){
//printf("**********%d %d\n",coef,expn);
p = (LNode*)malloc(sizeof(LNode));
p ->coef = coef;
p ->expn = expn;
printf("\n该项初始化完成,该项为:\n");
printf(" %dx^%d \n",p->coef,p ->expn);
q ->next = p;
printf("请输入该项的系数:\n");
scanf("%d",&coef);
//printf("注意:您输入的系数为:%d",coef);
printf("请输入该项的幂:\n");
scanf("%d",&expn);
//printf("注意:您输入的幂为:%d\n",expn);
}
q ->next = NULL;
}

void LinkListPlus(LinkList &La,LinkList &Lb){
LinkList pa,pb;
pa = La ->next;
pb = Lb;
while(pa && pb){
pb = Lb ->next;
while(pb){
if(pa ->expn == pb ->expn){
pa ->coef = pa ->coef + pb ->coef;
break;
}
else
pb = pb ->next;
}
while(!pb){
LinkList q = pb;
pb ->next = pa ->next;
pa ->next = pb;
pb = q ->next;
}
pa = pa ->next;
}
}


/*void LinkListPlus(LinkList &La,LinkList &Lb){
LinkList pre,pa,pb,u;
pre = La ->next;
pa = La ->next;  
pb = Lb ->next;
while(pa && pb){
if(pa ->expn < pb ->expn){
pre = pa;
pa = pa ->next;
}
else if(pa ->expn == pb ->expn){
int x = pa ->coef + pb ->coef;
if(x){
pa ->coef = x;
pre = pa;
}
else{
pre ->next = pa ->next;
free(pa);
}
pa = pre ->next;
u = pb;
pb = pb ->next;
free(pb);
}
else{
u = pb ->next;
pb ->next = pa;
pre ->next = pb;
pre = pb;
pb = u;
}
if(pb)
pre ->next = pb;
free(Lb);

}*/
int _tmain(int argc, _TCHAR* argv[])
{
LinkList La,Lb,l;
printf("构造第一个方程式: \n");
//printf("**********");
InitLinkList(La);
//printf("**********");
CreateLinkList(La);
l = La->next;
while(l->next != NULL){
printf(" %dx^%d \n",l->coef,l ->expn);
l = l ->next;
}

}

问什么最后不能输出呢,那个地方错了呢???求大神给解答!!!

[解决办法]
用gcc环境调的,你不要直接拷贝,我修过的地方标记了,主要是创建链表时没能链接起来。

C/C++ code
//#include "stdafx.h"#include "stdio.h"#include "stdlib.h"typedef struct LNode{ // 项的表示,多项式的项作为LinkList的数据元素int coef;//系数int expn;//指数struct LNode *next;}*LinkList;void InitLinkList(LinkList &L){//建立链表的头结点L = (LNode*)malloc(sizeof(LNode));if(!L)printf("Error");L->next = NULL;}void CreateLinkList(LinkList &L){int coef;int expn;LinkList q,p;q = L;printf("请输入该项的系数:\n");scanf("%d",&coef);//printf("注意:您输入的系数为:%d\n",coef);printf("请输入该项的幂:\n");scanf("%d",&expn);//printf("注意:您输入的幂为:%d\n",expn);while(coef != 0 || expn != 0){//printf("**********%d %d\n",coef,expn);p = (LNode*)malloc(sizeof(LNode));p ->coef = coef;p ->expn = expn;printf("\n该项初始化完成,该项为:\n");printf(" %dx^%d \n",p->coef,p ->expn);q ->next = p;q=p;//---->这一句是关键,每次q为了记住p的上一个节点,所以每次新建p都要更新q。printf("请输入该项的系数:\n");scanf("%d",&coef);//printf("注意:您输入的系数为:%d",coef);printf("请输入该项的幂:\n");scanf("%d",&expn);//printf("注意:您输入的幂为:%d\n",expn);}q ->next = NULL;}void LinkListPlus(LinkList &La,LinkList &Lb){LinkList pa,pb;pa = La ->next;pb = Lb;while(pa && pb){pb = Lb ->next;while(pb){if(pa ->expn == pb ->expn){pa ->coef = pa ->coef + pb ->coef;break;}elsepb = pb ->next;}while(!pb){LinkList q = pb;pb ->next = pa ->next;pa ->next = pb;pb = q ->next;}pa = pa ->next;}}/*void LinkListPlus(LinkList &La,LinkList &Lb){LinkList pre,pa,pb,u;pre = La ->next;pa = La ->next;   pb = Lb ->next;while(pa && pb){if(pa ->expn < pb ->expn){pre = pa;pa = pa ->next;}else if(pa ->expn == pb ->expn){int x = pa ->coef + pb ->coef;if(x){pa ->coef = x;pre = pa;}else{pre ->next = pa ->next;free(pa);}pa = pre ->next;u = pb;pb = pb ->next;free(pb);}else{u = pb ->next;pb ->next = pa;pre ->next = pb;pre = pb;pb = u;}if(pb)pre ->next = pb;free(Lb);}  }*/int main(int argc, char* argv[]){LinkList La,Lb,l;printf("构造第一个方程式: \n");//printf("**********");InitLinkList(La);//printf("**********");CreateLinkList(La);l = La->next;while(l!= NULL){//--->printf(" %dx^%d \n",l->coef,l ->expn);l = l ->next;}} 

热点排行