首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

看看如何合并两个链表,帮忙修改一上

2012-09-08 
看看怎么合并两个链表,帮忙修改一下#includestdio.h#includestdlib.hstruct student{int xuehaostruc

看看怎么合并两个链表,帮忙修改一下
#include<stdio.h>
#include<stdlib.h>

struct student{
int xuehao;
struct student *next;
};
void disp(struct student *head)
{
struct student *p=head->next;
while(p!=NULL)
{
printf("%d ",p->xuehao);
p=p->next;
}
}

struct student * build(){
struct student *head,*pnew,*t;
int i,k=0;
head=(struct student*)malloc(sizeof(struct student));
head->next=NULL;
t=head;
if(head==NULL)
return NULL;
while(1)
{
pnew=(struct student*)malloc(sizeof(struct student));
printf("input xuehao");
scanf("%d",&i);
if(i==-1) break;
pnew->xuehao=i;
pnew->next=t->next;
t->next=pnew;
t=pnew;
}
return head;
}

void px(struct student *head)
{
struct student *p=head->next,*t,*q;
int k;
for(;p!=NULL;p=p->next)
{
t=p;

for(q=t->next;q!=NULL;q=q->next)
{
if(t->xuehao>q->xuehao)
t=q;
}
if(t!=p)
{
k=p->xuehao;
p->xuehao=t->xuehao;
t->xuehao=k;
}
}
}
void lj(struct student *heada,struct student *headb)
{
struct student *pa=heada->next,*pb=headb->next,*t,*q;
while(pb!=NULL)
{
if(pa->xuehao < pb->xuehao)
{
while(pa->next->xuehao < pb->xuehao)
{
pa=pa->next;
}
t=pb;
while(pa->next->xuehao > pb->next->xuehao&&pb->next!=NULL)
{
pb=pb->next;
}
q=pb->next;
pb->next=pa->next;
pa->next=t;
pa=pb->next;
pb=q;

}
if(pa->xuehao > pb->xuehao)
{
t=pb;
while(pb->next->xuehao < pa->xuehao&&pb->next!=NULL)
{
pb=pb->next;
}
q=pb->next;
pb->next=pa->next;
pa->next=t;
pa=pb->next;
pb=q;
}

}
}











void main()
{
struct student *heada,*headb;
printf("input a 链表\n");
heada=build();
px(heada);
printf("a 链表 排序为");
disp(heada);

printf("\n\n");
printf("input b 链表\n");
headb=build();
px(headb);
printf("b 链表 排序为\n");
disp(headb);
printf("\n\n");
lj(heada,headb);
printf("合并的链表===");
disp(heada);

}




[解决办法]

C/C++ code
#include<stdio.h>#include<stdlib.h>struct student{    int xuehao;    struct student *next;};void disp(struct student *head)//遍历链表函数{     struct student *p=head->next;    while(p!=NULL)    {        printf("%d ",p->xuehao);        p=p->next;    }}struct student * build()//链表的建立!!!{    struct student *head,*pnew,*t;    int i,k=0;    head=(struct student*)malloc(sizeof(struct student));    head->next=NULL;    t=head;    if(head==NULL)        return NULL;    while(1)    {        pnew=(struct student*)malloc(sizeof(struct student));        printf("input xuehao");        scanf("%d",&i);        if(i==-1) break;        pnew->xuehao=i;        pnew->next=NULL;//这里你错了!!        t->next=pnew;        t=pnew;     }    return head;}void px(struct student *head)//对链表进行冒泡排序!!{    struct student *p=head->next,*t,*q;    int k;    for(;p!=NULL;p=p->next)    {        t=p;                for(q=t->next;q!=NULL;q=q->next)        {            if(t->xuehao>q->xuehao)                t=q;        }        if(t!=p)        {            k=p->xuehao;            p->xuehao=t->xuehao;            t->xuehao=k;        }     }}struct student  *lj(struct student *heada,struct student *headb)//看不懂楼主的合并算法!!自己写了一个最主流的{    struct student *pa,*pb,*pc,*headc;    pa=heada->next;    pb=headb->next;    headc=pc=heada;    while(pa&&pb)    {        if(pa->xuehao<pb->xuehao)        {            pc->next=pa;            pc=pa;            pa=pa->next;        }        else        {            pc->next=pb;            pc=pb;            pb=pb->next;        }    }        pc->next=pa?pa:pb;        free(headb);    return headc; }void main(){    struct student *heada,*headb,*h;    printf("input a 链表\n");    heada=build();    px(heada);    printf("\na 链表 排序为");    disp(heada);        printf("\n\n");    printf("input b 链表\n");    headb=build();    px(headb);    printf("\nb 链表 排序为\n");    disp(headb);    printf("\n\n");    printf("合并的链表===");    h=lj(heada,headb);    disp(h);    } 

热点排行