【数据结构】noj004 单链表的插入

#include

#include

typedef struct Node{

int data;

struct Node*next;

}Node,*linklist;

void create(linklist *l,int n)

{

linklist p,s;

p=*l=(linklist)malloc(sizeof(Node));

for(;n>0;n--)

{

s=(linklist)malloc(sizeof(Node));

scanf("%d",&s->data);

p->next=s,p=s;

}

p->next=NULL;

}

void shuchu(linklist l)

{

linklist p=l->next;

while(p!=NULL)

{

printf("%d ",p->data);

p=p->next;

}

}

linklist merge(linklist la,linklist lb)

{

Node *pa;

Node *pb;

Node *s;

linklist lc;

pa=la->next;

pb=lb->next;

lc=la;

lc->next=NULL;

while(pa&&pb)

{

if(pa->datadata )

{

s=pa;pa=pa->next;

s->next=lc->next;

lc->next=s;

}

else

{

s=pb;pb=pb->next;

s->next=lc->next;

lc->next=s;

}

}

while(pa)

{

s=pa;

pa=pa->next;

s->next=lc->next;

lc->next=s;

}

while(pb)

{

s=pb;

pb=pb->next;

s->next=lc->next;

lc->next=s;

}

return (lc);

}

int main()

{

linklist la,lb;

int m,n;

la=(linklist)malloc(sizeof(Node));

scanf("%d%d",&m,&n);

create(&la,m);

create(&lb,n);

shuchu(merge(la,lb));

return 0;

}

你可能感兴趣的:(【数据结构】noj004 单链表的插入)