#ifndef _Node_
#define _Node_
typedef struct _node {
int number;
struct _node *next;
}Node;
#endif // ! _node_
#ifndef _List_
#define _List_
#include "Node.h"
typedef struct _list {
Node *head;
Node *tail;
}List;
#endif // ! _list_
#include <stdio.h>
#include <stdlib.h>
#include "Node.h"
#include "List.h"
//循环链表的相关函数
void add_list(List *list, int num) { //添加链表的节点
Node *p = (Node *)malloc(sizeof(Node));
p->number = num;
p->next = NULL;
if (list->head) {
list->tail->next = p;
}
else {
list->head = p;
}
list->tail = p;
}
void creat_list(List *list) { //构建一个链表
int num = 0;
while (true)
{
printf("input the number\n");
scanf("%d", &num);
if (num != -1) {
add_list(list, num);
}
else {
break;
}
}
list->tail->next = list->head;
}
void print_list(List list) { //完成输入后打印整串链表
while (list.head != list.tail) {
printf("%d\n", list.head->number);
list.head = list.head->next;
}
printf("%d\n", list.head->number);
}
void union_list(List list1, List list2, List *list3) { //将两个循环链表合并成为一个
list1.tail->next = list2.head;
list3->head = list1.head;
list3->tail = list2.tail;
list3->tail->next = list3->head;
}
int main() {
List list_1;
list_1.head = list_1.tail = NULL;
creat_list(&list_1);
printf("The list_1 is:\n");
print_list(list_1);
printf("List 2:\n");
List list_2;
list_2.head = list_2.tail = NULL;
creat_list(&list_2);
printf("The list_2 is:\n");
print_list(list_2);
List list_3;
list_3.head = list_3.tail = NULL;
union_list(list_1, list_2,&list_3);
printf("The New list is:\n");
print_list(list_3);
return 0;
}