-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinked_list_obj.c
161 lines (139 loc) · 4.14 KB
/
linked_list_obj.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "objects.h"
#include "linked_list_obj.h"
/*
Lib array of definition of linked list for objects
by Goran Topic
*/
List* list_make(void){
/* allocate a memory for the head of the list */
List* list = malloc(sizeof(List));
list->count =0;
list->first = NULL;
return list;
}
void node_free(Node* node){
free_obj(node->obj);
free(node);
}
void list_free(List* list){
/* free up the memory used in the whole of the list*/
Node* current = list->first;
Node* prev = current;
while(current->next != NULL){
prev = current;
current = current->next;
node_free(prev);
}
node_free(current);
free(list);
}
void list_append(List* list, Object* obj){
/* adds and obj to the list */
//make new node
Node* new_node = malloc(sizeof(Node));
new_node->next = NULL;
new_node->obj = obj;
obj->parent_list = (List*)list;
if(list->count == 0){
list->first = new_node;
}else{
Node* current = list->first;
while(current->next != NULL) current = current->next;
current->next = new_node;
}
list->count++;
}
void list_print(List* list){
/* prints the contento of the list of objs */
if(list->first == NULL || list->count == 0){
//link list is empty
printf("ERROR: could not print empty link list\n");
return;
}
Node* current = list->first;
while(true) {
printf(" %s \n", current->obj->title); //print title
for(int line = 0; line < current->obj->model_c; line++)
printf("%s\n", current->obj->model[line]);
printf("\n");
if(current->next != NULL) current = current->next;
else break;
}
printf("count: %d\n", list->count);
}
void list_remove_at(List* list, int index){
/*removes an element from the list at the index position and frees it's memory*/
if(index >= list->count){
printf("ERROR: could not remove at index greater then count of elemesnts\n");
return;
}
Node* current = list->first;
Node* prev = current;
for(int i = 0; i < index; i++){
prev = current;
current = current->next;
}
if(prev != current) prev->next = current->next; //if is is not the first node
else list->first = current->next; //if ti is the frist node
//eliminate remove the node from the list
list->count--;
node_free(current);
}
void list_remove_title(List* list, char* title){
/*removes an element from the list at the if it matches the given title and frees it's memory*/
if(list->first == NULL || list->count == 0){
//link list is empty
printf("ERROR: could find title obj to remove in empty link list\n");
return;
}
Node* current = list->first;
Node* prev = current;
while(true){
if(strcmp(current->obj->title, title) == 0){
printf("found: title: %s \n", current->obj->title);
//node title found
if(prev == current) list->first = current->next; //if ti is the frist node
else prev->next = current->next; //if is is not the first node
//eliminate remove the node from the list
list->count--;
node_free(current);
return;
}
if(current->next != NULL){
prev = current;
current = current->next;
}else break;
}
printf("ERROR: title: %s could not be found\n", title);
}
Object* list_get_at(List* list, int index){
/*removes an element from the list at the index position and frees it's memory*/
if(index >= list->count){
printf("ERROR: could not get obj at index greater then count of elemesnts\n");
return NULL;
}
Node* current = list->first;
for(int i = 0; i < index; i++){
current = current->next;
}
return current->obj;
}
Object* list_get_title(List* list, char* title){
/*removes an element from the list at the if it matches the given title and frees it's memory*/
if(list->first == NULL || list->count == 0){
//link list is empty
printf("ERROR: could find title obj to remove in empty link list\n");
return NULL;
}
Node* current = list->first;
while(true){
if(strcmp(current->obj->title, title) == 0) return current->obj;
if(current->next != NULL) current = current->next;
else break;
}
printf("ERROR: title: %s could not be found\n", title);
}