In: Computer Science
list.h file
#ifndef LIST_H_
#define LIST_H_
struct ListNode {
long value;
struct ListNode *next;
};
struct ListNode *list_prepend(struct ListNode *list, long value);
int list_length(struct ListNode *list);
struct ListNode *list_remove(struct ListNode *list, long
value);
#endif /* LIST_H_ */
given.c file
#include
#include "list.h"
struct ListNode *list_prepend(struct ListNode *list, long value)
{
struct ListNode *node = malloc(sizeof(struct ListNode));
node->value = value;
node->next = list;
return node;
}
list.c file
#include
#include "list.h"
/* Counts and returns the number of nodes in the given list.
*/
int list_length(struct ListNode *list) {
return -1;
}
/* Searches the given list for the first node containing the
given
* value. If such a list element is found, it is removed from the
list
* and freed
* Returns the resulting list after any adjustments are made.
*/
struct ListNode *list_remove(struct ListNode *list, long value)
{
return NULL;
}
list.c
#include "list.h"
#include<stdio.h>
#include<stdlib.h>
/* Counts and returns the number of nodes in the given list.
*/
int list_length(struct ListNode *list) {
int count = 0;
while(list != NULL){
count++;
list = list->next;
}
return count;
}
/* Searches the given list for the first node containing the
given
* value. If such a list element is found, it is removed from the
list
* and freed
* Returns the resulting list after any adjustments are made.
*/
struct ListNode *list_remove(struct ListNode *list, long value)
{
if(list == NULL) return NULL;
struct ListNode* current = list,*prev = NULL;
if(list->value == value){
list = list->next;
free(current);
return list;
}
while(current != NULL && current->value !=
value){
prev = current;
current = current->next;
}
if(current == NULL) return list;
prev->next = current->next;
free(current);
return list;
}