单链表的合并
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node *list;
list creat();
list hebing(list l1,list l2);
void print(struct node *head);
int main(void){
list l1,l2;
struct node *head;
l1=creat();
l2=creat();
head=hebing(l1,l2);
print(head);
return 0;
}
list creat(){
int x;
struct node head,p,tail;
scanf("%d",&x);
head=(struct node)malloc(sizeof(struct node));
head->next=NULL;
p=tail=head;
while(x!=-1){
p=(struct node)malloc(sizeof(struct node));
p->data=x;
tail->next=p;
tail=p;
scanf("%d",&x);
}
tail->next=NULL;
return head;
}
list hebing(list l1,list l2){
list t,s,head,p;
head=(struct node)malloc(sizeof(struct node));
head->next=NULL;
p=head;
t=l1->next;
s=l2->next;
while(t!=NULL&&s!=NULL){
if(t->datadata){
p->next=t;
t=t->next;
}
else{
p->next=s;
s=s->next;
}
p=p->next;}
if(!t){
p->next=s;
}
else{
p->next=t;
}
return head;
}
void print( list head){
struct node *p;
p=head->next;
if(p==NULL){
printf(“NULL”);
}
while§{
printf("%d",p->data);
p=p->next;
}
}