###General Comments:
General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
Comments on Delete:
###Comments on Delete: YouYou are leaking the list item when you delete it.
Since the create_item()
is calling malloc()
I would expect the delete_item()
to call free()
.
I would split the delete_item()
into two parts. The first part that deals with the head as a special case and the second part that deals with removing elements from the list (and free()
ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item()
is calling malloc()
I would expect the delete_item()
to call free()
.
I would split the delete_item()
into two parts. The first part that deals with the head as a special case and the second part that deals with removing elements from the list (and free()
ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}
General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
Comments on Delete:
You are leaking the list item when you delete it.
Since the create_item()
is calling malloc()
I would expect the delete_item()
to call free()
.
I would split the delete_item()
into two parts. The first part that deals with the head as a special case and the second part that deals with removing elements from the list (and free()
ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item()create_item()
is calling mallocmalloc()
I would expect the delete_item()delete_item()
to call freefree()
.
I would split the delete_item()delete_item()
into two parts. The first part that deals with the head as a special case and the second part that deals with removing elemntselements from the list (and free()free()
ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item() is calling malloc I would expect the delete_item() to call free.
I would split the delete_item() into two parts. The first part that deals with the head as a special case and the second part that deals with removing elemnts from the list (and free()ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item()
is calling malloc()
I would expect the delete_item()
to call free()
.
I would split the delete_item()
into two parts. The first part that deals with the head as a special case and the second part that deals with removing elements from the list (and free()
ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item() is calling malloc I would expect the delete_item() to call free.
I would split the delete_item() into two parts. The first part that deals with the head as a special case and the second part that deals with removing elemnts from the list (and free()ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter;iter->next;
}
else
{
printf("deleting element: %i\n", x);
last.next->next = iter.next;->next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item() is calling malloc I would expect the delete_item() to call free.
I would split the delete_item() into two parts. The first part that deals with the head as a special case and the second part that deals with removing elemnts from the list (and free()ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter;
}
else
{
printf("deleting element: %i\n", x);
last.next = iter.next;
}
free(iter);
return head;
}
###General Comments:
Why do your list handling functions not take a list as a parameter?
As a result your application can only have one list.
###Comments on Delete: You are leaking the list item when you delete it.
Since the create_item() is calling malloc I would expect the delete_item() to call free.
I would split the delete_item() into two parts. The first part that deals with the head as a special case and the second part that deals with removing elemnts from the list (and free()ing them).
void delete_item(struct node** list, int x)
{
if ((list == NULL) || ((*list) == NULL))
{
printf("not found\n");
return;
}
(*list) = delete_item_from_list(*list);
}
struct node* delete_item_from_list(struct node* head)
{
struct node* iter = head;
struct node* last = NULL;
while (iter != NULL)
{
if (iter->x == x)
{
break;
}
last = iter;
iter = iter->next;
}
if (iter == NULL)
{
printf("not found\n");
}
else if (last == NULL)
{
printf("found in first element: %i\n", x);
head = iter->next;
}
else
{
printf("deleting element: %i\n", x);
last->next = iter->next;
}
free(iter);
return head;
}