2012 42

#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
	char data;
	struct Node* next;
}Node, *List;

//分成三段创建链表
Node* CreateList(List L, int length)
{
	//printf("Please input\n");
	Node* node1;
	Node* rear;
	rear = L;
	//尾插法创建节点
	for (int i = 0; i < length; i++)
	{
		node1 = (Node*)malloc(sizeof(Node));
		//这样进行输入的话,空格也算字符

		scanf("%c", &node1->data);
		node1->next = rear->next;
		rear->next = node1;
		rear = node1;
	}
	return rear;
	//printf("\n");
}
Node* CreateCommon(Node* node,int length)
{
	if (length < 1)
	{
		free(node);
		return NULL;
	}
	else
	{
		Node* head;
		Node* rear;
		scanf("%c", &node->data);
		node->next = NULL;
		head = rear = node;
		for (int j = 1; j < length; j++)
		{
			node = (Node*)malloc(sizeof(Node));
			scanf("%c", &node->data);
			node->next = rear->next;
			rear->next = node;
			rear = node;
		}
		return head;
	}
	
	
	
}
void Print(List L)
{
	Node* node;
	node = L->next;
	while (node)
	{
		printf("%c", node->data);
		node = node->next;
	}
	printf("\n");
}
int Count(List L)
{
	int count = 0;
	Node* node = L->next;
	while (node)
	{
		count++;
		node = node->next;
	}
	return count;
}
int main()
{
	//创建表头
	List L1, L2;
	L1 = (List)malloc(sizeof(Node));
	L2 = (List)malloc(sizeof(Node));
	L1->next = NULL;
	L2->next = NULL;
	Node* common;
	common = (Node*)malloc(sizeof(Node));
	Node* rear1 = CreateList(L1, 4);//尾节点
	Node* rear2 = CreateList(L2, 3);//尾节点
	common = CreateCommon(common, 3);//头结点
	rear1->next = common;
	rear2->next = common;
	//Print(L1);
	//千万注意,不能分开接受,分开打印。系统的工作过程是先进行输入,在进行输出
	/*CreateList(L2, 3);
	CreateList(L1, 4);
	Print(L1);
	Print(L2);*/
	//Node* rear1 = CreateList(L1, 4);
	//Node* rear2 = CreateList(L2, 3);
	
	//指针作为参数,必须进行初始化
	
	/*测试
	while (common)
	{
		printf("%c", common->data);
		common = common->next;
	}
	*/
	int count1 = Count(L1);
	int count2 = Count(L2);
	Node* p; p = L1;
	Node* q; q = L2;
	if (count1 > count2)
	{
		int t = count1 - count2;
		for (int i = 0; i < t; i++)
		{
			p = p->next;
		}
		while (p&&p != q)
		{
			p = p->next;
			q = q->next;
		}
		printf("%c", p->data);
	}
	else
	{
		int t = count2 - count1;
		for (int i = 0; i < t; i++)
		{
			q = q->next;
		}
		while (p&&p != q)
		{
			p = p->next;
			q = q->next;
		}
		printf("%c", p->data);
	}
	
	

	return 0;
}

 

posted @ 2021-09-29 13:46  磐正  阅读(20)  评论(0)    收藏  举报