循环链表

2 篇文章 0 订阅
#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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值