标题:请教合并链表的程序问题
只看楼主
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
 问题点数:0 回复次数:3 
请教合并链表的程序问题

#include<iostream>
using namespace std;

class Element
{
public:
int number;
Element *point;
};

Element *Init()
{
Element *curret,*first;
first=curret=new Element;
int count=0;
while(count<4)
{
cout<<"输入数据:";
cin>>curret->number;
curret=curret->point=new Element;
count++;
}
curret->point=NULL;
return first;
}

void print(Element *p)
{
Element *curret;
curret=p;
cout<<"\n全部的数据是:";
while(curret->point!=NULL)
{
cout<<curret->number<<" ";
curret=curret->point;
}
cout<<endl;
}

void function(Element *a,Element *b)
{
Element *Acurret,*Bcurret,*c,*temp,*lc;
Acurret=a->point;
Bcurret=b->point;
lc=c=a;
lc->point=NULL;
while(Acurret->point!=NULL && Bcurret->point!=NULL)
{
if(Acurret->number<Bcurret->number)
{
temp=Acurret->point;
Acurret->point=lc->point;
lc->point=Acurret;
Acurret=temp;
}
else
{
temp=Bcurret->point;
Bcurret->point=lc->point;
lc->point=Bcurret;
Bcurret=temp;
}
}
while(Bcurret->point!=NULL)
{
temp=Bcurret->point;
Bcurret->point=lc->point;
lc->point=Bcurret;
Bcurret=temp;
}
print(c->point);
}

void main()
{
Element *PA,*PB,*Ahead,*Bhead;
Bhead=Ahead=new Element();
PA=Init();
Ahead->point=PA;
cout<<"\nA链表输入完毕!\n\n";
PB=Init();
Bhead->point=PB;
cout<<"\nB链表输入完毕!\n";
print(PA);
print(PB);
function(Ahead,Bhead);
}
各位大哥大姐帮我看看这个程序错在哪里呢?谢谢!!!

搜索更多相关主题的帖子: 链表 Element curret count 
2006-10-13 20:47
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
得分:0 

nodelink link_head12(nodelink head1,nodelink head2)
{ nodelink q,head3,s,p;
head3=(nodelink)malloc(sizeof(node));/*建head3表*/
head3->next=NULL;
q=NULL;
s=head1->next;
p=head2->next;
while(p&&s) /*排序*/
{ if(s->info<p->info)
{ q=s->next;
s->next=head3->next;
head3->next=s;
s=q;
}
else
{ q=p->next;
p->next=head3->next;
head3->next=p;
p=q;
}
}
if(s)
{
while(s->next!=NULL)
{ q=s;
s->next=head3->next;
head3->next=s;
s=q->next;
}
s->next=head3->next;
head3->next=s;
}
else
{ while(p->next!=NULL)
{ q=p;
p->next=head3->next;
head3->next=p;
p=q->next;
}
p->next=head3->next;
head3->next=p;
}
return head3;
}

倚天照海花无数,流水高山心自知。
2006-10-13 21:16
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
得分:0 
题目要求利用原表,不能生成新表!!!
2006-10-14 20:25
zkkpkk
Rank: 2
等 级:论坛游民
威 望:5
帖 子:489
专家分:28
注 册:2006-6-17
得分:0 

很简单,将一条链表的指向NULL(也就是最后)的结点的指针狱指向第二条链表的头指针指向的指针狱指向的数据狱,就可以了(狂喘气ing)


Viva,espana!
2006-10-17 17:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-96218-1-1.html




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

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