/*
Welcome to JDoodle!
You can execute code here in 88 languages. Right now you’re in the C IDE.
1. Click the orange Execute button ▶ to execute the sample code below and see how it works.
2. Want help writing or debugging code? Type a query into JDroid on the right hand side ---------------->
3. Try the menu buttons on the left. Save your file, share code with friends and open saved projects.
Want to change languages? Try the search bar up the top.
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct list {
int data;
struct list *next;
} list;
list *item_ahead(list *l, list *x) {
if ((l == NULL) || (l->next == NULL)) {
return NULL;
}
if ((l->next) == x) {
return l;
} else {
return item_ahead(l->next, x);
}
}
void delete_list(list **l, list **x) {
list *p; /* Указатель на узель*/
list *pred; /* Указатель на предшествуюrщлй узел*/
p = *l;
pred = item_ahead(*l, *x);
if (pred == NULL) { /* Соединяем список */
// printf("% d \n", *p->data);
*l = p->next;
} else {
pred->next = (*x)->next;
}
free(*x); /*Освобождаем память узла*/
}
list *create_node(int data) {
list *node = (list *)malloc(sizeof(list));
if (node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
node->data = data;
node->next = NULL;
return node;
}
void print_list(list *head) {
list *current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
// Создаем список: 1 -> 2 -> 3 -> 4 -> NULL
list *head = create_node(1);
head->next = create_node(2);
head->next->next = create_node(3);
head->next->next->next = create_node(4);
printf("Original list: ");
print_list(head);
// // Удаляем узел со значением 3 (не головной)
// list *to_delete = head->next->next;
// printf("\nDeleting node with value %d\n", to_delete->data);
// delete_list(head, to_delete);
// printf("List after deletion: ");
// print_list(head);
delete_list(&head, &head);
printf("List after deletion: ");
print_list(head);
// list *not_exist = create_node(0);
// delete_list(&head, ¬_exist);
// printf("List after deletion: ");
// print_list(head);
// // Удаляем головной узел (со значением 1)
// to_delete = head;
// printf("\nDeleting head node with value %d\n", to_delete->data);
// delete_list(head, to_delete);
// printf("List after deletion: ");
// print_list(head);
// to_delete = NULL;
// printf("\nDeleting head node with value %d\n", to_delete->data);
// delete_list(head, to_delete);
// printf("List after deletion: ");
// print_list(head);
return 0;
}