关于连续释放链表空间的问题,求指导!
是一元多项式运算的程序,在建立了各个链表之后,在最后想撤销它们,可是连续调用六遍会出错,但是调用两遍却没事,不知是链表的问题还是del()函数的问题,下面是我的程序,希望大侠能给出指导!(问题出在main()最后的del()上 )程序代码:
#include<iostream> #include<cmath> using namespace std; typedef struct node{ float a; int b; struct node *next; }lnode,*linklist; linklist creat_linklist(int k){ linklist H; lnode *p,*r; int max,mid; float mark; H=r=NULL; for(int i=0;i<k;i++){ cout<<"请输入第"<<i+1<<"项的<系,指>:"; cin>>mark>>mid; while(mark==0){ cout<<"系数不能为零,请重新输入<系,指>:"; cin>>mark>>mid; } if(i==0){ max=mid; } else{ while(mid<=max){ cout<<endl; cout<<"请按照指数依次升高的原则重新输入<系,指>:"; cin>>mark>>mid; } } max=mid; p=new lnode; if(!p){cout<<"分配空间错误!"<<endl; exit(1);} p->a=mark; p->b=max; cout<<endl; if(H==NULL) H=p; else r->next=p; r=p; } r->next=NULL;//若链表末尾不设置为空,其他函数将无法正常终止循环,因为s1=s1->next无法正常结尾; cout<<endl; return H; } void output(linklist H){ lnode *s=NULL; s=H; while(s){ if(s!=H&&s->a>0){ if(s->b==1&&s->a==1) cout<<"+x"; else if(s->b==1){ cout<<"+"<<s->a<<"x"; } else if(s->b==0) cout<<"+"<<s->a; else if(s->a==1) cout<<"+"<<"x^"<<s->b; else cout<<"+"<<s->a<<"x^"<<s->b; } else{ if(s==H&&s->a==0&&s->next==NULL) cout<<"0"; else if(s->a==0){ s=s->next; continue; } else if(s->b==0){ cout<<s->a; } else if(s->b==1&&s->a==1) cout<<"x"; else if(s->b==1&&s->a==-1) cout<<"-x"; else if(s->b==1) cout<<s->a<<"x"; else if(s->a==-1) cout<<"-x^"<<s->b; else if(s->a==1) cout<<"x^"<<s->b; else cout<<s->a<<"x^"<<s->b; } s=s->next; } } linklist add(linklist H1,linklist H2){ linklist H3=NULL; lnode *s1,*s2,*r; lnode *pre; int k=-1; s1=H1; s2=H2; r=H3=NULL; while(s1!=NULL&&s2!=NULL){ if(s1->b==s2->b){ if(s1->a+s2->a==0){ if(s1->next==NULL&&s2->next==NULL){ k=0; s1=s1->next; s2=s2->next; } else if(s1->next==NULL&&s2->next!=NULL){ s1=s1->next; s2=s2->next; k=1; // continue; } else if(s2->next==NULL&&s1!=NULL){ s1=s1->next; s2=s2->next; k=2; // continue; } else{ s1=s1->next; s2=s2->next; // continue; } } else{ pre=new lnode; pre->a=s1->a+s2->a; pre->b=s1->b; if(H3==NULL) H3=pre; else r->next=pre; r=pre; r->next=NULL;//此处同上处一样,若不将新建链表末尾置为NULL关于其循环将无法正常终止!!! s1=s1->next; s2=s2->next; } } else if(s1->b<s2->b){ pre=new lnode; pre->a=s1->a; pre->b=s1->b; if(H3==NULL) H3=pre; else r->next=pre; r=pre; s1=s1->next; } else { pre=new lnode; pre->a=s2->a; pre->b=s2->b; if(H3==NULL) H3=pre; else r->next=pre; r=pre; s2=s2->next; } } if(k==0){ if(H3==NULL){ H3=new lnode; H3->a=0; H3->b=0; H3->next=NULL; } r->next=NULL; } else if(k==1&&s2!=NULL){ pre=new lnode; pre->a=s2->a; pre->b=s2->b; if(H3==NULL) H3=pre; else r->next=pre; r=pre; r->next=NULL; } else if(k==2&&s1!=NULL){ pre=new lnode; pre->a=s1->a; pre->b=s1->b; if(H3==NULL) H3=pre; else r->next=pre; r=pre; r->next=NULL; } else if(s1!=NULL) r->next=s1; else r->next=s2; return H3; } linklist sub(linklist H1,linklist H2){ linklist H4=NULL; lnode *s1,*s2,*r; lnode *pre; int k=-1; s1=H1; s2=H2; r=H4=NULL; while(s1!=NULL&&s2!=NULL){ if(s1->b==s2->b){ if(s1->a-s2->a==0){ if(s1->next==NULL&&s2->next==NULL){ k=0; s1=s1->next; s2=s2->next; } else if(s1->next==NULL&&s2->next!=NULL){ s1=s1->next; s2=s2->next; k=1; // continue; } else if(s2->next==NULL&&s1->next!=NULL){ s1=s1->next; s2=s2->next; k=2; // continue; } else{ s1=s1->next; s2=s2->next; // continue; } } else{ pre=new lnode; pre->a=s1->a-s2->a; pre->b=s1->b; if(H4==NULL) H4=pre; else r->next=pre; r=pre; r->next=NULL;//此处同上处一样,若不将新建链表末尾置为NULL关于其循环将无法正常终止!!! s1=s1->next; s2=s2->next; } } else if(s1->b<s2->b){ pre=new lnode; pre->a=s1->a; pre->b=s1->b; if(H4==NULL) H4=pre; else r->next=pre; r=pre; s1=s1->next; } else { pre=new lnode; pre->a=-s2->a; pre->b=s2->b; if(H4==NULL) H4=pre; else r->next=pre; r=pre; s2=s2->next; } } if(k==0){ if(H4==NULL){ H4=new lnode; H4->a=0; H4->b=0; H4->next=NULL; } else r->next=NULL; } else if(k==1&&s2!=NULL){ while(s2!=NULL){ pre=new lnode; pre->a=-s2->a; pre->b=s2->b; if(H4==NULL) H4=pre; else r->next=pre; r=pre; r->next=NULL; s2=s2->next; } } else if(k==2&&s1!=NULL){ pre=new lnode; pre->a=s1->a; pre->b=s1->b; if(H4==NULL) H4=pre; else r->next=pre; r=pre; r->next=NULL; } else if(s1!=NULL){ r->next=s1; while(s1!=NULL){ s1->a=s1->a; s1=s1->next; } } else{ r->next=s2; while(s2!=NULL){ s2->a=-s2->a; s2=s2->next; } } return H4; } linklist der(linklist H){ lnode *p,*s; p=H; while(p){ if(p->b==0){ if(p==H){ s=p; p=p->next; H=p; delete s; } else{ p->a=0; p=p->next; } } else{ p->a=p->a*p->b; p->b=p->b-1; p=p->next; } } return H; } void del(linklist H){ lnode *p,*s; p=H; while(p){ s=p; p=p->next; delete s; } } int main(){ linklist H1=NULL,H2=NULL,H3=NULL,H4=NULL,H5=NULL,H6=NULL; int m,n; cout<<" 温馨提示:多项式请按指数依次递增的规则输入"<<endl; cout<<"请输入多项式a的项数"; cin>>m; while(m<=0){ cout<<endl; cout<<"不符合数学逻辑,请重新输入:"; cin>>m; } H1=creat_linklist(m); cout<<"请输入多项式b的项数"; cin>>n; while(n<=0){ cout<<endl; cout<<"不符合数学逻辑,请重新输入:"; cin>>n; } H2=creat_linklist(n); cout<<"多项式a为:"; output(H1); cout<<endl; cout<<"多项式b为:"; output(H2); cout<<endl; H3=add(H1,H2); cout<<"相加的结果为:"; output(H3); cout<<endl; cout<<"相减的结果为:"; H4=sub(H1,H2); output(H4); cout<<endl; cout<<"对a的求导结果:"; H5=der(H1); output(H5); cout<<endl; cout<<"对b的求导结果:"; H6=der(H2); output(H6); cout<<endl; del(H1); del(H2); del(H3); del(H4); del(H5); del(H6); cout<<"各链表空间释放完毕!!!"<<endl; return 0; }
[ 本帖最后由 泡vs泡happy 于 2013-12-21 22:24 编辑 ]