#include <stdlib.h>
#include "list.h"
struct node *init(struct door door) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node != NULL) {
new_node->door = door;
new_node->next = NULL;
}
return new_node;
}
struct node *add_door(struct node *elem, struct door door) {
struct node *new_node = NULL;
if (elem != NULL) {
new_node = (struct node *)malloc(sizeof(struct node));
if (new_node != NULL) {
new_node->door = door;
new_node->next = elem->next;
elem->next = new_node;
}
}
return new_node;
}
struct node *find_door(int door_id, struct node *root) {
struct node *current = root;
struct node *result = NULL;
int found = 0;
while (current != NULL && found == 0) {
if (current->door.id == door_id) {
result = current;
found = 1;
} else {
current = current->next;
}
}
return result;
}
struct node *remove_door(struct node *elem, struct node *root) {
struct node *current = root;
struct node *prev = NULL;
struct node *new_root = root;
int found = 0;
if (elem == NULL || root == NULL) {
new_root = root;
} else if (elem == root) {
new_root = root->next;
free(root);
} else {
while (current != NULL && found == 0) {
if (current == elem) {
prev->next = current->next;
free(current);
found = 1;
} else {
prev = current;
current = current->next;
}
}
}
return new_root;
}
void destroy(struct node *root) {
struct node *current = root;
struct node *next_node;
while (current != NULL) {
next_node = current->next;
free(current);
current = next_node;
}
}