-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
91 lines (84 loc) · 2.08 KB
/
vector.c
File metadata and controls
91 lines (84 loc) · 2.08 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
//
// Created by Aref on 19/06/30.
//
#include <malloc.h>
#include "vector.h"
#include "etc.h"
Vector *pushback(Vector *head, int data_size) {
Vector *new = (Vector *) malloc(sizeof(Vector) * 1);
if (data_size == 0)
new->data = NULL;
else
new->data = malloc(data_size * 1);
new->next = NULL;
if (head == NULL)
return new;
while (head->next != NULL)
head = head->next;
head->next = new;
return new;
}
void *index(Vector *head, int i, int data_size) {
// while (i--)
// head = head->next;
// return head->data;
int j = 0;
for (Vector *it = head; it != NULL; it = it->next) {
if (it->data == NULL)
continue;
if (j == i)
return it->data;
j += 1;
}
return NULL;
}
int length_list(Vector *head) {
int n = 0;
for (Vector *it = head; it != NULL; it = it->next)
if (it->data != NULL)
n += 1;
return n;
}
void free_vector(Vector *head) {
for (Vector *it = head; it != NULL;) {
free(it->data);
Vector *tmp = it;
it = it->next;
free(tmp);
}
}
Object *find_by_point(Vector *head, Point x) {
if (same_point(x, INVALID_POINT))
return NULL;
for (Vector *it = head; it != NULL; it = it->next) {
if (it->data == NULL) continue;
Object *obj = (Object *) it->data;
if (same_point(obj->point, INVALID_POINT))
continue;
if(same_point(obj->point, x))
return obj;
}
return NULL;
}
void clean_vector(Vector *head) {
Vector* last = head;
head = head->next;
for (Vector *it = head; it != NULL;) {
if(it->data == NULL){
last->next = it->next;
Vector* tmp = it;
it = it->next;
free(tmp);
continue;
}
if(same_point(((Object*) it->data)->point,INVALID_POINT)){
last->next = it->next;
Vector* tmp = it;
it = it->next;
free(tmp);
continue;
}
last = last->next;
it = it->next;
}
}