-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternateIterator.c
More file actions
99 lines (80 loc) · 1.41 KB
/
Copy pathAlternateIterator.c
File metadata and controls
99 lines (80 loc) · 1.41 KB
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
#include "AlternateIteratorHeader.h"
struct node {
int position;
int value;
node* next;
node* previous;
};
struct list {
int size;
node* new_node;
};
list* create_list()
{
list* new_list = (list*)malloc(sizeof(list));
new_list->size = 0;
new_list->new_node = NULL;
return new_list;
}
void append_list_rec(node* list_node, int value)
{
if (list_node->next == NULL)
{
node* new_node = (node*)malloc(sizeof(node));
new_node->value = value;
new_node->next = NULL;
new_node->previous = list_node;
new_node->position = list_node->position + 1;
list_node->next = new_node;
}
else
{
append_list_rec(list_node->next, value);
}
}
bool append_list(list* list, int value)
{
if (list == NULL)
{
return false;
}
else if (list->size == 0)
{
node* temp = (node*)malloc(sizeof(node));
temp->value = value;
temp->next = NULL;
temp->previous = NULL;
temp->position = 0;
list->new_node = temp;
}
else
{
append_list_rec(list->new_node, value);
}
list->size += 1;
return list;
}
bool alternateIterator(node* node)
{
if (node->position % 2 == 0)
return true;
return false;
}
void print(node* node)
{
if (node == NULL)
return;
if (alternateIterator(node))
printf("%d\n", node->value);
print(node->next);
}
int main(void) {
list* new_list = create_list();
for (int i = 0; i < 100; i++)
{
append_list(new_list, i);
}
print(new_list->new_node);
getchar();
return EXIT_SUCCESS;
}