-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathQ2-5.CPP
122 lines (114 loc) · 1.91 KB
/
Q2-5.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*********************************
题目描述:
求一个有环的单链表中环开始处的节点
Date:2014-03-26
**********************************/
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}Node,*pNode;
#include<stdio.h>
#include<stdlib.h>
/*
定义一个速度为2的快指针,一个速度为1的慢指针,
如果链表中有环,则返回两个指针相遇的节点,
如果没有链表中没有环,则返回NULL。
*/
pNode WetherCircle(pNode pHead)
{
if(!pHead)
return NULL;
pNode p1 = pHead;
pNode p2 = pHead;
//直到二者相遇,退出循环
while((p1 && p2 && p1!=p2) || (p1==p2 && p1==pHead && p2==pHead))
{
p1 = p1->next;
p2 = p2->next->next;
}
if(p1 == p2)
return p1;
else
return NULL;
}
/*
计算环中的节点个数
*/
int CircleLen(pNode pHead)
{
pNode p = WetherCircle(pHead);
if(!p)
return 0;
int count = 1;
//固定一个指针,另一个指针在环中移动
pNode p1 = p->next;
while(p1 != p)
{
count++;
p1 = p1->next;
}
return count;
}
/*
求环开始的节点
*/
pNode CircleBegin(pNode pHead)
{
int len = CircleLen(pHead);
if(len < 1)
return NULL;
pNode p1 = pHead;
pNode p2 = pHead;
int i;
//第一个指针先移动len个节点
for(i=0;i<len;i++)
p1 = p1->next;
//而后一起移动
while(p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
/*
建立如下所示的带环的单链表
1->2->3->4->5->6->7->4
即环的入口节点为date域为4的节点
*/
pNode create_CircleList()
{
pNode pHead = (pNode)malloc(sizeof(Node));
if(!pHead)
{
printf("malloc faild!\n");
exit(-1);
}
pHead->data = 1;
pNode r = pHead;
int i;
for(i=0;i<6;i++)
{
pNode pNew = (pNode)malloc(sizeof(Node));
if(!pNew)
{
printf("malloc faild!\n");
exit(-1);
}
pNew->data = i + 2;
r->next = pNew;
r = pNew;
}
//将最后一节点的next指向第四个节点,形成环
r->next = pHead->next->next->next;
return pHead;
}
int main()
{
pNode pHead = create_CircleList();
pNode p = CircleBegin(pHead);
printf("The date in the beginNode of the Circle is:%d\n",p->data);
return 0;
}