LINUX.ORG.RU

История изменений

Исправление alysnix, (текущая версия) :

чисто для хохмы попросил чатгпт выполнить запрос

«write in c removing element from a single linked list»

вижу прекрасное наличие указателя prev в функции removeNode. о чем и весь сырбор.

код не проверял, но вроде, если по диагонали - похоже.

#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node
struct Node {
    int data;
    struct Node* next;
};

// Function to remove a node with a given key
void removeNode(struct Node** head_ref, int key) {
    // Store head node
    struct Node* temp = *head_ref;
    struct Node* prev = NULL;

    // If head node itself holds the key to be deleted
    if (temp != NULL && temp->data == key) {
        *head_ref = temp->next; // Changed head
        free(temp); // Free old head
        return;
    }

    // Search for the key to be deleted, keep track of the previous node
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    // If key was not present in linked list
    if (temp == NULL) return;

    // Unlink the node from linked list
    prev->next = temp->next;

    free(temp); // Free memory
}

// Function to push a new node to the linked list
void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

// Function to print the linked list
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;

    // Create the linked list 1->2->3->4->5
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);

    printf("Linked list before removal: ");
    printList(head);

    // Remove node with data 3
    removeNode(&head, 3);

    printf("Linked list after removal: ");
    printList(head);

    return 0;
}

вот тут в цикле проходится список и апдейтится prev. вот тебе и «квантор общности».

 while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

также обратите внимание что каши-малы с указателями на указатель там нет. ибо это чудачество (нужно неких спец. алгоритмах, народе сборщиков мусора или перемещения обьектов), но это точно не проход по списку. пишите чем проще, тем лучше.

если что - двойной указатель в параметрах функции - это просто var параметр типа указатель. в си по другому не запишешь.

Исправление alysnix, :

чисто для хохмы попросил чатгпт выполнить запрос

«write in c removing element from a single linked list»

вижу прекрасное наличие указателя prev в функции removeNode. о чем и весь сырбор.

код не проверял, но вроде, если по диагонали - похоже.

#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node
struct Node {
    int data;
    struct Node* next;
};

// Function to remove a node with a given key
void removeNode(struct Node** head_ref, int key) {
    // Store head node
    struct Node* temp = *head_ref;
    struct Node* prev = NULL;

    // If head node itself holds the key to be deleted
    if (temp != NULL && temp->data == key) {
        *head_ref = temp->next; // Changed head
        free(temp); // Free old head
        return;
    }

    // Search for the key to be deleted, keep track of the previous node
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    // If key was not present in linked list
    if (temp == NULL) return;

    // Unlink the node from linked list
    prev->next = temp->next;

    free(temp); // Free memory
}

// Function to push a new node to the linked list
void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

// Function to print the linked list
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;

    // Create the linked list 1->2->3->4->5
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);

    printf("Linked list before removal: ");
    printList(head);

    // Remove node with data 3
    removeNode(&head, 3);

    printf("Linked list after removal: ");
    printList(head);

    return 0;
}

вот тут в цикле проходится список и апдейтится prev. вот тебе и «квантор общности».

 while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

Исходная версия alysnix, :

чисто для хохмы попросил чатгпт выполнить запрос

«write in c removing element from a single linked list»

вижу прекрасное наличие указателя prev в функции removeNode. о чем и весь сырбор.

код не проверял, но вроде, если по диагонали - похоже.

#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node
struct Node {
    int data;
    struct Node* next;
};

// Function to remove a node with a given key
void removeNode(struct Node** head_ref, int key) {
    // Store head node
    struct Node* temp = *head_ref;
    struct Node* prev = NULL;

    // If head node itself holds the key to be deleted
    if (temp != NULL && temp->data == key) {
        *head_ref = temp->next; // Changed head
        free(temp); // Free old head
        return;
    }

    // Search for the key to be deleted, keep track of the previous node
    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    // If key was not present in linked list
    if (temp == NULL) return;

    // Unlink the node from linked list
    prev->next = temp->next;

    free(temp); // Free memory
}

// Function to push a new node to the linked list
void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

// Function to print the linked list
void printList(struct Node* node) {
    while (node != NULL) {
        printf("%d -> ", node->data);
        node = node->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = NULL;

    // Create the linked list 1->2->3->4->5
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);

    printf("Linked list before removal: ");
    printList(head);

    // Remove node with data 3
    removeNode(&head, 3);

    printf("Linked list after removal: ");
    printList(head);

    return 0;
}