有问题 总是溢出 高手帮忙看看
#include <iostream.h>
struct student
{int ID;
float score;
student *next;
};
student *head1=NULL,*head2=NULL,*head=NULL;
student *creatlist(student *h)
{student*p,*s;
cout < < "输入学生学号及成绩 " < <endl;
s=new student;
cin> > s-> ID> > s-> score;
head=NULL;
while(s-> ID!=0)
{if(h==NULL)
h=s;
else p-> next=s;
p=s;
s=new student;
cin> > s-> ID> > s-> score;
}
p-> next=NULL;
delete s;
return h;
}
void showlist(student*h)
{cout < < "现在开始输出链表内容 " < <endl;
while(h)
{cout < <h-> ID < < '\t ' < <h-> score < <endl;
h=h-> next;
}
cout < <endl;
}
student* sort(student*head1,student*head2) //按顺序连接两个链表并改序号
{student*p=head1;
int i=1;
head=head1;
if(head1-> score <head2-> score) //如果第一个序列的小,就不插入//
{head1-> ID=i;
head1=head1-> next;
i++;
}
else //相反第一个数列的大就插入序列二的节点//
{head2-> ID=i;
p=p-> next;
head1=head2;
head2=head2-> next;
head1-> next=p;head1=p;
i++;
}
return head;
}
void main()
{cout < < "说明:输入链表内容,按升序排列,学号从1开始\n\n " < <endl;
cout < < "现在输入第一个链表 " < <endl;
showlist(creatlist(head1));
cout < < "现在输入第二个链表 " < <endl;
showlist(creatlist(head2));
cout < < "现在输出合并后的序列: " < <endl;
showlist(sort(head1,head2));
}
[解决办法]
#include <iostream.h>
struct student
{
int ID;
float score;
student *next;
};
student *head1=NULL,*head2=NULL,*head=NULL;
student *creatlist(student *h)
{
student*p,*s;
p = h;
cout < < "输入学生学号及成绩 " < <endl;
s=new student;
cin> > s-> ID> > s-> score;
head=NULL;
while(s-> ID!=0)
{
if(h==NULL)
h=s;
else
p-> next=s;
p=s;
s=new student;
cin> > s-> ID> > s-> score;
}
p-> next=NULL;
delete s;
return h;
}
void showlist(student*h)
{
cout < < "现在开始输出链表内容 " < <endl;
while(h)
{
cout < <h-> ID < < '\t ' < <h-> score < <endl;
h=h-> next;
}
cout < <endl;
}
student* sort(student*head1,student*head2) //按顺序连接两个链表并改序号
{
student*p=head1;
student *head,*q;
int i=1;
head=head1;
/*if(head1-> score <head2-> score) //如果第一个序列的小,就不插入//
{
head1-> ID=i;
head1=head1-> next;
i++;
}
else //相反第一个数列的大就插入序列二的节点//
{
head2-> ID=i;
p=p-> next;
head1=head2;
head2=head2-> next;
head1-> next=p;head1=p;
i++;
} */
while (head1!=NULL && head2!=NULL)
{
if(head1-> score <head2-> score) //如果第一个序列的小,就不插入//
{
if ( i == 1)
{
head1-> ID = i;
p = head1;
head1 = head1-> next;
i++;
}
else
{
head1-> ID=i;
p-> next = head1;
p = head1;
head1 = head1-> next;
i++;
}
}
else //相反第一个数列的大就插入序列二的节点//
{
/*head2-> ID=i;
p=p-> next;
head1=head2;
head2=head2-> next;
head1-> next=p;head1=p;
i++; */
if ( i == 1)
{
head2-> ID = i;
p = head2;
head = head2;
head2 = head2-> next;
i++;
}
else
{
head2-> ID = i;
p-> next = head2;
p = head2;
head2 = head2-> next;
i++;
}
}
}
if (head1 != NULL)
{
q = head1;
while (head1 != NULL)
{
head1-> ID = i;
i++;
head1 = head1-> next;
}
}
else
{
q = head2;
while ( head2 != NULL )
{
head2-> ID = i;
i++;
head2 = head2-> next;
}
}
p-> next = q;
return head;
}
void main()
{
cout < < "说明:输入链表内容,按升序排列,学号从1开始\n\n " < <endl;
cout < < "现在输入第一个链表 " < <endl;
head1 = creatlist(head1);
//showlist(creatlist(head1));
showlist(head1);
cout < < "现在输入第二个链表 " < <endl;
head2 = creatlist(head2);
//showlist(creatlist(head2));
showlist(head2);
cout < < "现在输出合并后的序列: " < <endl;
showlist(sort(head1,head2));
}