R7-1 两个有序链表合并(新表不含重复元素)
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。
要求S3中没有重复元素。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出合并后新的非降序链表,要求链表中没有重复元素。数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。
代码如下:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int date; //数据域
struct Node* next; //指针域
};
//创建链表
struct Node* creatList() {
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL; //初始化
return headNode;
};
//创建结点
struct Node* creatNode(int date) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->date = date; //初始化
newNode->next = NULL;
return newNode;
};
//打印
void printLsit(struct Node* headNode) {
struct Node* pMove = headNode->next;
while (pMove) {
printf("%d ", pMove->date);
pMove = pMove->next;
}
printf("\n");
}
//插入结点(头插)
void insertNodeByHead(struct Node* headNode, int date) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->next = headNode->next;
headNode->next = newNode;
newNode->date = date;
}
//尾插法
void insertNodebyRear(struct Node* headList, int data) {
struct Node *newNode = creatNode(data);
while (headList->next) {
headList = headList->next;
}
newNode->next = NULL;
headList->next = newNode;
}
//struct Node* endofList(struct Node *List)
//{
// while(List->next) List = List->next;
// return List;
//}
//
插入结点(尾插)
//void insertNodeByTrall(struct Node *List,int data)
//{
// struct Node *lastNode = endofList(List);
// struct Node *newNode = creatNode(data);
// lastNode->next = newNode;
//}
//刪除指定结点
void deleteNodeByAppion(struct Node* headNode, int date) {
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
printf("无法删除,链表为空");
else {
while (posNode->date != date) {
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL) {
printf("没有找到对应结点,无法删除");
break;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
struct Node* hebing(struct Node* List1, struct Node* List2) {
struct Node* L3 = creatList();
struct Node* L1 = List1->next;
struct Node* L2 = List2->next;
int lastdate = -1;
while (L1 != NULL || L2 != NULL) {
if (L1 == NULL) {
if (L2->date == lastdate)
L2 = L2->next;
else {
insertNodebyRear(L3, L2->date);
lastdate = L2->date;
L2 = L2->next;
}
} else if (L2 == NULL) {
if (L1->date == lastdate)
L1 = L1->next;
else {
insertNodebyRear(L3, L1->date);
lastdate = L1->date;
L1 = L1->next;
}
} else if (L1->date < L2->date) {
if (L1->date == lastdate) {
L1 = L1->next;
} else {
insertNodebyRear(L3, L1->date);
lastdate = L1->date;
L1 = L1->next;
}
} else if (L1->date == L2->date) {
if (L1->date == lastdate) {
L1 = L1->next;
L2 = L2->next;
} else {
insertNodebyRear(L3, L1->date);
lastdate = L1->date;
L1 = L1->next;
L2 = L2->next;
}
} else {
if (L2->date == lastdate)
L2 = L2->next;
else {
insertNodebyRear(L3, L2->date);
lastdate = L2->date;
L2 = L2->next;
}
}
}
return L3;
}
int main() {
struct Node* list1 = creatList();
int a;
while (1) {
scanf("%d", &a);
if (a == -1)break;
insertNodebyRear(list1, a);
}
printf("第一个链表:");
printLsit(list1);
struct Node* list2 = creatList();
while (1) {
scanf("%d", &a);
if (a == -1)break;
insertNodebyRear(list2, a);
}
printf("第二个链表:");
printLsit(list2);
struct Node* list3 = creatList();
list3 = hebing(list1, list2);
printf("合并后的链表:");
printLsit(list3);
system("pause");
}