| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 430 人关注过本帖
标题:关于链表合并的问题
只看楼主 加入收藏
c盘格式化
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-4-3
收藏
 问题点数:0 回复次数:1 
关于链表合并的问题

题目,建立a,b两条链表,每个结点包括学号,成绩.要合并两条链表,按学号升序排列.
请各位师兄帮我看我的答案哪里出错了,我找了很久都找不出.
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define TURE 1
#define LEN sizeof(struct student)
struct student
{
int num;
int score;
struct student *next;
};
int n;
struct student *cread(void)
{
struct student *head,*p1,*p2;
n=0;
head=NULL;
while(TURE)
{
p1=(struct student*)malloc(LEN);
n++;
cout<<"please iput the numble:";
cin>>p1->num;
cout<<"score:";
cin>>p1->score;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
if(p1->num==0)
{
p2->next=NULL;
break;
}
}

return(head);
}
struct student *insert(struct student *head_a,struct student *head_b)
{
struct student *a1,*a2,*b;
a1=head_a;
b=head_b;
do
{
while((b->num>a1->num)&&(a1->next!=NULL))
{
a2=a1;
a1=a1->next;
}
if(b->num<=a1->num)//插入前面或中间
{
if(head_a==a1)
head_a=b;
else
a2->next=b;
b->next=a1;
a2=b;
}
else//插入链表最后
{
a1->next=b;
break;
}
}
while((b=b->next)!=NULL);
return(head_a);
}


void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do
{
cout<<"numble:"<<p->num;
cout<<endl;
cout<<"score:"<<p->score;
cout<<endl;
p=p->next;
}while(p->next!=NULL);
}
int main()
{
struct student *a;
struct student *b;
struct student *c;//指向合并后的链表
a=cread();
b=cread();
c=insert(a,b);
print(c);

return 0;
}

搜索更多相关主题的帖子: 链表 
2007-05-20 16:50
c盘格式化
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2007-4-3
收藏
得分:0 
有没有高手帮我看一下啊
2007-05-21 14:43
快速回复:关于链表合并的问题
数据加载中...
 
   



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

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