//库函数头文件包含
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//函数状态码定义
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType; //假设线性表中的元素均为整型
typedef struct LNode
{
ElemType data;
struct LNode *next;
} LNode, *LinkList;
//打印链表中的数据
void ListPrint_L(LinkList &L)
{
if (L->next == NULL)
{
printf("NULL");
return;
}
//输出单链表
LNode *p = L->next; // p指向第一个元素结点
while (p != NULL)
{
if (p->next != NULL)
printf("%d ", p->data);
else
printf("%d", p->data);
p = p->next;
}
}
//为链表添加结点,需要传入直接前驱
LNode *addLNode(LinkList &L, ElemType e)
{
L->next = (LinkList)malloc(sizeof(LNode));
if (!(L->next))
{
exit(OVERFLOW);
}
L->next->data = e;
L->next->next = NULL;
return L->next;
}
Status ListCreate_L(LinkList &L)
{
L = (LNode *)malloc(sizeof(LNode));
L->next = NULL;
if (!L)
{
exit(OVERFLOW);
}
LNode *p = L;
int num = 0;
while (scanf("%d", &num) != EOF)
{
if (num == -1)
{
break;
}
p = addLNode(p, num);
}
return OK;
}
LNode *LinkOrderList(LinkList &L1, LinkList &L2)
{
LNode *head = (LNode *)malloc(sizeof(LNode));
head->next = NULL;
//列表有一个为空的情况
if ((L1->next) && (L2->next))
{
LNode *s = L1, *t = L2, *p = head;
while ((s->next) && (t->next))
{
if ((s->next->data) > (t->next->data))
{
t = t->next;
}
else if ((s->next->data) < (t->next->data))
{
s = s->next;
}
else
{
p = addLNode(p, s->next->data);
s = s->next;
t = t->next;
}
}
}
return head;
}
int main()
{
LNode *L1 = (LNode *)malloc(sizeof(LNode)), *L2 = (LNode *)malloc(sizeof(LNode));
ListCreate_L(L1);
ListCreate_L(L2);
LNode *head = LinkOrderList(L1, L2);
ListPrint_L(head);
return 0;
}