单链表操作之(判断是否带环,求环长,求入口节点)点击打开链接
#define _CRT_SECURE_NO_WARNINGS 1
typedef int DataType;
#define NULL 0
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef struct Node
{
DataType data;
struct Node*next;
}Node, *LinkList;
void InitList(LinkList *L)
{
*L = (Node*)malloc(sizeof(Node));
if (*L == NULL){
printf("申请内存空间失败");
}
(*L)->next = NULL;
}
LinkList BuyNode(DataType data)
{
LinkList NewNode = NULL;
NewNode = (LinkList)malloc(sizeof(Node));
if (NewNode == NULL){
printf("为节点创建空间失败");
}
NewNode->data = data;
NewNode->next = NULL;
return NewNode;
}
void PrintList(LinkList L)
{
LinkList Cur = L;
if (L == NULL){
printf("NULL");
}
Cur = Cur->next;
while (Cur)
{
if (Cur == NULL)
return;
printf("%d--->", Cur->data);
Cur = Cur->next;
}
printf("NULL\n");
}
void PushBack(LinkList* L, DataType data)
{
assert(L);
LinkList Cur = *L;
if (Cur == NULL){
Cur = BuyNode(data);
}
while (Cur->next)
{
Cur = Cur->next;
}
Cur->next = BuyNode(data);
}
int Size(LinkList L){
assert(L);
LinkList Cur = L;
int count = 0;
if (Cur->next == NULL)
return 1;
while (Cur->next){
Cur = Cur->next;
count++;
}
return count;
}
LinkList Find(LinkList L, DataType data)
{
assert(L);
if (L == NULL){
return NULL;
}
LinkList Cur = L;
while (Cur != NULL)
{
if (Cur->data == data){
return Cur;
}
Cur = Cur->next;
}
return NULL;
}
LinkList HasCircle(LinkList L)
{
LinkList Fast = L;
LinkList Slow = L;
while (Fast&&Fast->next){
Fast = Fast->next->next;
Slow = Slow->next;
if (Slow == Fast)
return Slow;
}
return NULL;
}
//判断不带环的链表是否相交两种情况Y,V尾节点相同
int IsCrossNodeWithoutCircle1(LinkList L1, LinkList L2)
{
LinkList PL1=L1;
LinkList PL2=L2;
if (L1 == NULL || L2 == NULL)
return 0;
while(PL1->next)
{
PL1 = PL1->next;
}
while(PL2->next){
PL2 = PL2->next;
}
if (PL1==PL2)
return 1;
else
return 0;
}
//判断不带环的链表是否相交两种情况Y,用L1的尾节点连接L2的头节点如果L1带环则L1与l2相交
int IsCrossNodeWithoutCircle2(LinkList L1, LinkList L2)
{
LinkList PL1 = L1;
LinkList PL2 = L2;
if (L1 == NULL || L2 == NULL)
return 0;
while (PL1->next)
{
PL1 = PL1->next;
}
PL1->next = PL2;
if (NULL!=HasCircle(L1))
return 1;
else
return 0;
}
test5()
{
LinkList L = NULL;
LinkList M = NULL;
LinkList S = NULL;
InitList(&L);
InitList(&M);
PushBack(&L, 1);
PushBack(&L, 2);
PushBack(&L, 4);
PushBack(&M, 5);
PushBack(&M, 6);
S = Find(M, 6);
S->next = Find(L, 2);
printf("%d %d", IsCrossNodeWithoutCircle1(L, M), Size(L));
}
//求相交节点。。。先让长的链表遍历L1-L2,再让连个链表一起遍历当L1=L2时L1和L2都是相交节点
LinkList GetCrossNodeWithoutCircle(LinkList L1, LinkList L2)
{
if (0 == IsCrossNodeWithoutCircle1(L1, L2))
return NULL;
int size1 = Size(L1);
int size2 = Size(L2);
LinkList PL1 = L1;
LinkList PL2 = L2;
int gap = size1 - size2;
if (gap > 0){
while (gap--){
PL1 = PL1->next;
}
}
else
{
while (gap++){
PL2 = PL2->next;
}
}
while (PL1!=PL2){
PL1 = PL1->next;
PL2 = PL2->next;
}
return PL1;
}
//PL1 PL2 0---》代表不带环,1--》代表带环
//0 0 俩不带环单链表相交
//0 1 不存在
//1 0 不存在
//1 1 环内相交,环外相交
int IsCrossWithCircle(LinkList L1,LinkList L2) //1代表俩无环单链表相交,2代表俩有环单链表相交
{
LinkList MeetNode1 = HasCircle(L1);
LinkList MeetNode2 = HasCircle(L2);
if (NULL == MeetNode1 || NULL == MeetNode2)
{
if (IsCrossNodeWithoutCircle2(L1, L2)!= NULL)
return 1;
}
else if (MeetNode1&&MeetNode2)
{
LinkList Cur = MeetNode1;
while (Cur->next != MeetNode1){
if (Cur == MeetNode2)
return 2;
Cur = Cur->next;
}
if (Cur == MeetNode2)
return 2;
}
}
test6()
{
LinkList L = NULL;
LinkList M = NULL;
LinkList S = NULL;
InitList(&L);
InitList(&M);
PushBack(&L, 1);
PushBack(&L, 2);
PushBack(&L, 4);
PushBack(&M, 5);
PushBack(&M, 6);
S = Find(M, 6);
S->next = Find(L, 2);
printf("%d", IsCrossNodeWithoutCircle2(L, M));
}
test7()
{
LinkList L = NULL;
LinkList M = NULL;
LinkList S = NULL;
InitList(&L);
InitList(&M);
PushBack(&L, 1);
PushBack(&L, 2);
PushBack(&L, 4);
PushBack(&M, 5);
PushBack(&M, 6);
S = Find(M, 6);
S->next = Find(L, 2);
LinkList Q=GetCrossNodeWithoutCircle(L, M);
printf("%d", Q->data);
}
test8()
{
LinkList L = NULL;
LinkList M = NULL;
LinkList S = NULL;
InitList(&L);
InitList(&M);
PushBack(&L, 1);
PushBack(&L, 2);
PushBack(&L, 3);
PushBack(&L, 4);
PushBack(&L, 5);
S = Find(L, 5);
S->next = Find(L, 3);
PushBack(&M, 6);
PushBack(&M, 7);
S = Find(M, 7);
S->next = Find(L, 3);
printf("%d", IsCrossWithCircle(L,M));
}
int main()
{
test8();
//test7();
//test6();
//test5();
//test4();
//test3();
system("pause");
return 0;
}