-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzad6jc.cpp
76 lines (60 loc) · 1.18 KB
/
zad6jc.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
#include<stdio.h>
#include<stdlib.h>
typedef struct _node {
int val;
struct _node *next;
} node;
typedef node* lista;
lista push(int val, lista l){
lista nowy = (node*)malloc(sizeof(node));
nowy->val = val;
nowy->next = l;
return nowy;
}
void printList(lista l){
while(l){
printf("%i ", l->val);
l = l->next;
}
printf("\n");
}
int odwrocIPolicz(lista l){
lista poprzedni = NULL, nast;
int ct = 0;
while(l){
nast = l->next;
l->next = poprzedni;
poprzedni = l;
l = nast;
ct++;
}
return ct;
}
int dlugoscCylku(lista l){
int kroki = odwrocIPolicz(l), ct = 0;
for(int i = 0; i < kroki / 2 + 1; i++){
l = l->next; //jestem na cyklu
}
lista start = l;
do
{
ct++;
l = l->next;
} while (l != start);
odwrocIPolicz(l);
return ct;
}
int main(){
lista l = NULL, mem, pocz;
pocz = l = push(7, l);
l = push(6, l);
l = push(5, l);
l = push(4, l);
mem = l = push(3, l);
l = push(2, l);
l = push(1, l);
pocz->next = mem;
printf("%i\n", dlugoscCylku(l));
// printList(l);
return 0;
}