@@ -48,12 +48,16 @@ int list_insert(struct list **l, size_t index, int e){
|
||||
|
||||
if(!l) return 1;
|
||||
|
||||
if (list_count(*l) < index) return 1;
|
||||
|
||||
struct list *new_node = malloc(sizeof(struct list));
|
||||
if(!new_node) return 1;
|
||||
|
||||
new_node->data = e;
|
||||
if(index == 0){
|
||||
|
||||
new_node->data = e;
|
||||
new_node->next = *l;
|
||||
*l = new_node;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct list *tmp = *l;
|
||||
size_t ind = 0;
|
||||
@@ -64,6 +68,9 @@ int list_insert(struct list **l, size_t index, int e){
|
||||
ind ++;
|
||||
}
|
||||
|
||||
if (!tmp) return 1;
|
||||
|
||||
new_node->data = e;
|
||||
new_node->next = tmp->next;
|
||||
tmp->next = new_node;
|
||||
|
||||
@@ -73,16 +80,15 @@ int list_insert(struct list **l, size_t index, int e){
|
||||
|
||||
struct list *list_get(struct list *l, size_t index){
|
||||
|
||||
if(!l) return NULL;
|
||||
//if(list_count(l) < index) return NULL;
|
||||
|
||||
struct list *tmp = l;
|
||||
size_t ind = 0;
|
||||
while(tmp->next != NULL && ind != index){
|
||||
|
||||
while(tmp != NULL && ind < index){
|
||||
tmp = tmp->next;
|
||||
ind ++;
|
||||
}
|
||||
if(ind >= index) return NULL;
|
||||
|
||||
return tmp;
|
||||
|
||||
}
|
||||
@@ -96,11 +102,9 @@ void list_destroy(struct list *l){
|
||||
|
||||
struct list *list_find(struct list *l, int e){
|
||||
|
||||
if(!l) return NULL;
|
||||
|
||||
struct list *tmp = l;
|
||||
|
||||
while (tmp->data != e) tmp = tmp->next;
|
||||
while (tmp != NULL && tmp->data != e) tmp = tmp->next;
|
||||
|
||||
return tmp;
|
||||
|
||||
@@ -108,6 +112,8 @@ struct list *list_find(struct list *l, int e){
|
||||
|
||||
struct list *list_delete_at(struct list **l, size_t index){
|
||||
|
||||
if (!l || !*l) return NULL;
|
||||
|
||||
struct list *old = *l;
|
||||
|
||||
if ( index == 0){
|
||||
@@ -118,46 +124,45 @@ struct list *list_delete_at(struct list **l, size_t index){
|
||||
struct list *tmp = *l;
|
||||
|
||||
size_t ind = 0;
|
||||
while(tmp->next != NULL && ind < index - 1){
|
||||
while(tmp != NULL && ind < index - 1){
|
||||
|
||||
tmp = tmp->next;
|
||||
ind ++;
|
||||
}
|
||||
|
||||
if (ind < index) {
|
||||
if (ind == index - 1) {
|
||||
old = tmp->next;
|
||||
tmp->next = old->next;
|
||||
return old;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
int list_remove(struct list **l, int e){
|
||||
|
||||
struct list *old = *l;
|
||||
struct list *old = NULL;
|
||||
struct list *tmp = *l;
|
||||
|
||||
while (tmp->next != NULL && tmp->data != e){
|
||||
while (tmp != NULL && tmp->data != e){
|
||||
|
||||
old = tmp;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
|
||||
if(tmp->data == e) {
|
||||
if (!tmp) return NULL;
|
||||
|
||||
old = tmp->next;
|
||||
tmp->next = old->next;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
if(!old) *l = tmp->next;
|
||||
else old->next = tmp->next;
|
||||
|
||||
free(tmp);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void list_print(struct list *l){
|
||||
|
||||
if(!l) {
|
||||
printf("\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
struct list *tmp = l;
|
||||
|
||||
while (tmp != NULL){
|
||||
@@ -169,6 +174,7 @@ void list_print(struct list *l){
|
||||
tmp = tmp->next;
|
||||
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user