王道数据结构链表算法题第二十三题

在这里插入图片描述

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>

//链表结构体
struct node
{
	char data;
	struct node* next;
};
//初始化链表
struct node* init_SingleList()
{
	struct node* singleList = malloc(sizeof(struct node));//分配空间
	if (singleList == NULL)
		return NULL;
	singleList->data = 0;
	singleList->next = NULL;

	return singleList;
}
//尾插法创建单链表
struct node* create_SingleListByTail()
{
	struct node* pmove, * pcreate, * head;
	char data, temp;
	int len;
	head = (struct node*)malloc(sizeof(struct node));
	
	if (head == NULL)
		return NULL;
	else
		head->next = NULL;
	pmove = head;
	printf("请输入要创建的单链表的长度:");
	scanf_s("%d", &len);
	if (len == 0)
		return NULL;
	printf("请输入数据:");
	
	for (int i = 0; i < len; i++)
	{
		
		temp = getchar();
		scanf_s("%c", &data, 1);
		pcreate = (struct node*)malloc(sizeof(struct node));
		if (pcreate == NULL)
			return;
		pcreate->data = data;
		pcreate->next = NULL;

		//插入操作
		pmove->next = pcreate;
		pmove = pcreate;
	}
	return head;
}
//将两个链表进行链接
struct node* link_SingleList(struct node* list1, struct node* list2)
{
	struct node* pmove,* qmove;
	pmove = list1;
	qmove = list2;
	while (pmove->next)
	{
		pmove = pmove->next;
	}
	pmove->next = qmove->next;  //更改list1的指针域,让其指向list2完成连接
	return list1;
}

//寻找两个单链表的相同后缀
struct node* findWordSameSuffix(struct node* list1, struct node* list2)
{
	struct node* tailA, * tailB;
	if (list1 == NULL || list2 == NULL)
		return NULL;
	tailA = list1->next;
	tailB = list2->next;
	int lenA=1, lenB=1;
	//求表长
	while (tailA->next != NULL)
	{
		lenA++;
		tailA = tailA->next;
	}
	while (tailB->next != NULL)
	{
		lenB++;
		tailB = tailB->next;
	}
	//当两链表的尾结点不一样时,说明没有公共的结点
	if (tailA != tailB)
		return NULL;

	//以下是一定有公共结点的情况
	struct node* longlist = NULL,* shortlist = NULL;//longlist指向长表,shortlist指向短表
	int dis = 0;//两表的长度差
	if (lenA > lenB)
	{
		longlist = list1->next;
		shortlist = list2->next;
		dis = lenA - lenB;
	}
	else
	{
		longlist = list2->next;
		shortlist = list1->next;
		dis = lenB - lenA;
	}
	//先让长表走两表长度差个位置
	while (dis--)
	{
		longlist = longlist->next;
	}
	//当长表走两表长度差个位置后,shortlist和longlist在一同走
	while (longlist != shortlist)
	{
		longlist = longlist->next;
		shortlist = shortlist->next;
	}

	return shortlist;
}
//打印
void printf_SingleList(struct node* singleList)
{
	struct node* pmove;
	pmove = singleList->next;
	while (pmove != NULL)
	{
		printf("%c", pmove->data);
		pmove = pmove->next;
	}
	printf("\n");
}

int main(void)
{
	struct node* listA, * listB, * listC, * AlinkC, * BlinkC,* commonNode;
	
	listA = init_SingleList();
	listB = init_SingleList();
	listC = init_SingleList();

	listA = create_SingleListByTail();
	printf("链表A中的数据为:\n");
	printf_SingleList(listA);

	
	listB = create_SingleListByTail();
	printf("链表B中的数据为:\n");
	printf_SingleList(listB);
	
	listC = create_SingleListByTail();
	if (listC != NULL)
	{
		printf("链表C中的数据为:\n");
		printf_SingleList(listC);
	}
	
	if (listC == NULL)
	{
		AlinkC = listA;
		printf("A表和C表链接后为:\n");
		printf_SingleList(AlinkC);
	}
	else
	{
		AlinkC = link_SingleList(listA, listC);
		printf("A表和C表链接后为:\n");
		printf_SingleList(AlinkC);
	}
	
	if (listC == NULL)
	{
		BlinkC = listB;
		printf("B表和C表链接后为:\n");
		printf_SingleList(BlinkC);
	}
	else
	{
		BlinkC = link_SingleList(listB, listC);
		printf("B表和C表链接后为:\n");
		printf_SingleList(BlinkC);
	}
	
	commonNode = findWordSameSuffix(AlinkC, BlinkC);//寻找两个单词的相同后缀
	if (commonNode == NULL)
		printf("两个链表没有共同的后缀!\n");
	else
		printf("两个链表共同后缀的起始位置为:%c\n", commonNode->data);

	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值