![]() |
#2
peach54602013-07-11 20:56
|
代码一:

#include<iostream>
using namespace std;
struct student{
int num;
int score;
student* next;
};
student * creat(int N){
student *ne;
ne=new student;
cin>>ne->num>>ne->score;
ne->next=NULL;
student*head;
head =ne;
student *p1;
p1=head;
student *p0;
for(int i=1;i!=N;++i){
ne=new student;
cin>>ne->num>>ne->score;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
}
return (head);
}
student *hebi(student* head,student *h2)
{
while(h2!=NULL){
student *p1,*ne,*p0;
ne=new student;
ne=h2;
p1=head;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
h2=h2->next;
}
return head;
}
void print(student * head){
student *p;
p=head;
do{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
int main(){
int N,M;
cin>>N>>M;
student *head1,*head2;
head1=creat(N);
head2=creat(M);
hebi(head1,head2);
print(head1);
return 0;
}
using namespace std;
struct student{
int num;
int score;
student* next;
};
student * creat(int N){
student *ne;
ne=new student;
cin>>ne->num>>ne->score;
ne->next=NULL;
student*head;
head =ne;
student *p1;
p1=head;
student *p0;
for(int i=1;i!=N;++i){
ne=new student;
cin>>ne->num>>ne->score;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
}
return (head);
}
student *hebi(student* head,student *h2)
{
while(h2!=NULL){
student *p1,*ne,*p0;
ne=new student;
ne=h2;
p1=head;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
h2=h2->next;
}
return head;
}
void print(student * head){
student *p;
p=head;
do{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
int main(){
int N,M;
cin>>N>>M;
student *head1,*head2;
head1=creat(N);
head2=creat(M);
hebi(head1,head2);
print(head1);
return 0;
}
自己感觉这样也是对的,但是输入数据之后,无法达到合并并排序的效果,
代码二

#include<iostream>
using namespace std;
struct student{
int num;
int score;
student* next;
};
student * creat(int N){
student *ne;
ne=new student;
cin>>ne->num>>ne->score;
ne->next=NULL;
student*head;
head =ne;
student *p1;
p1=head;
student *p0;
for(int i=1;i!=N;++i){
ne=new student;
cin>>ne->num>>ne->score;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
}
return (head);
}
student *hebi(student* head,student *h2)
{student *p1,*ne,*p0;
while(h2!=NULL){
ne=new student;
ne->num=h2->num;
ne->score=h2->score;
h2=h2->next;
p1=head;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
}
return (head);
}
void print(student * head){
student *p;
p=head;
do{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
int main(){
int N,M;
cin>>N>>M;
student *head1,*head2;
head1=creat(N);
head2=creat(M);
cout<<endl;
print(head1);
print(head2);
cout<<endl;
hebi(head1,head2);
print(head1);
return 0;
}
using namespace std;
struct student{
int num;
int score;
student* next;
};
student * creat(int N){
student *ne;
ne=new student;
cin>>ne->num>>ne->score;
ne->next=NULL;
student*head;
head =ne;
student *p1;
p1=head;
student *p0;
for(int i=1;i!=N;++i){
ne=new student;
cin>>ne->num>>ne->score;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
}
return (head);
}
student *hebi(student* head,student *h2)
{student *p1,*ne,*p0;
while(h2!=NULL){
ne=new student;
ne->num=h2->num;
ne->score=h2->score;
h2=h2->next;
p1=head;
while((ne->num>p1->num)&&(p1->next!=NULL)){
p0=p1;
p1=p1->next;
}
if(ne->num<=p1->num){
if(head==p1)head=ne;
else p0->next=ne;
ne->next=p1;
}else{
p1->next=ne;
ne->next=NULL;
}
}
return (head);
}
void print(student * head){
student *p;
p=head;
do{
cout<<p->num<<" "<<p->score<<endl;
p=p->next;
}while(p!=NULL);
}
int main(){
int N,M;
cin>>N>>M;
student *head1,*head2;
head1=creat(N);
head2=creat(M);
cout<<endl;
print(head1);
print(head2);
cout<<endl;
hebi(head1,head2);
print(head1);
return 0;
}
我把指针的数据先提出来,然后再插入,但在测试数据的时候
输入
2 3
2 4
1 2
3 2
4 1
5 2
输出
1 2
2 4
3 2
4 1
5 2
但输入
2 3
4 3
3 2
2 5
1 3
3 2
输出
3 2
4 3
以及其他数据都无法输出正确结果;
有的程序还会崩溃;
有谁能帮我看看问题出在哪;