-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlink_list_loop.c
118 lines (104 loc) · 1.88 KB
/
link_list_loop.c
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
#include <stdio.h>
#include <malloc.h>
struct list
{
int data;
struct list *next;
};
main()
{
struct list *head=NULL;
struct list *loop=NULL;
int loop_len=0;
list_append(&head,10);
list_append(&head,20);
list_append(&head,30);
list_append(&head,40);
list_append(&head,50);
list_append(&head,60);
list_append(&head,70);
list_append(&head,80);
loop= has_loop(&head);
printf("Loop at %x\n",loop);
loop_len=get_loop_length(loop);
printf("Length of the loop=%d\n",loop_len);
loop=get_merge_point(&head,loop_len);
printf("Data at merge=%d\n",loop->data);
print_list(&head);
}
int get_merge_point(struct list **head, int count)
{
struct list *start=*head;
struct list *end=start;
int i;
while(1)
{
end=start;
for(i=0;i<count;i++)
end=end->next;
printf("start=%d end=%d\n",start->data,end->data);
if(start==end) return start;
start=start->next;
}
}
int get_loop_length(struct list *loop)
{
struct list *start=loop;
int count=0;
while(loop && loop->next)
{
if(loop->next==start) return 1+count;
loop=loop->next;
count++;
}
}
int has_loop(struct list **head)
{
struct list *f=*head;
struct list *s=*head;
while(f && s)
{
f=f->next;
s=s->next->next;
if(f==s) return f;
}
return 0;
}
int list_append(struct list **head, int data)
{
static int i=0;
static struct list *spl=NULL;
++i;
struct list *start=*head;
struct list *temp;
if(start==NULL)
{
temp=malloc(sizeof(struct list));
temp->next=NULL;
*head=temp;
temp->data=data;
return;
}
while(start&&start->next)
{
start=start->next;
}
temp=malloc(sizeof(struct list));
temp->next=NULL;
start->next=temp;
temp->data=data;
if(i==4) spl=temp;
if(i==8) temp->next=spl;
}
int print_list(struct list **head)
{
struct list *start=*head;
int count=0;
while(start)
{
printf("Data=%d addr=%xnext=%x\n",start->data,start,start->next);
start=start->next;
count++;
if(count >=10) break;
}
}