C有序单链表的合并
#include <stdio.h>
#include <stdlib.h>
#include "../include/List.h"
Node* CombineList(Node* list1,Node *list2)
{
Node* p=list1->next;
Node* q=list2->next;
Node* list3=malloc(sizeof(int));
Node* last;
list3->next=NULL;
last=list3;
while(p!=NULL&&q!=NULL)
{
Node* newNode=malloc(sizeof(int));
if(p->data>=q->data)
{
newNode->data=q->data;
newNode->next=NULL;
last->next=newNode;
last=newNode;
q=q->next;
}
else
{
newNode->data=p->data;
newNode->next=NULL;
last->next=newNode;
last=newNode;
p=p->next;
}
}
if(p==NULL)
{
while(q!=NULL)
{
Node* newNode=malloc(sizeof(int));
newNode->data=q->data;
newNode->next=NULL;
last->next=newNode;
last=newNode;
q=q->next;
}
}
else
{
while(p!=NULL)
{
Node* newNode=malloc(sizeof(int));
newNode->data=p->data;
newNode->next=NULL;
last->next=newNode;
last=newNode;
p=p->next;
}
}
return list3;
}
int main(int argc, char *argv[]) {
Node* list1=CreateList();
Node* list2=CreateList();
Node* list3=CombineList(list1,list2);
PrintList(list3);
system("pause");
return 0;
}
建立和打印链表的头文件