current position:Home>Delete specific nodes of linked list (C language)

Delete specific nodes of linked list (C language)

2022-01-27 02:06:28 CSDN Q & A

The phenomenon and background of the problem

mooc, Problem description : Delete the specified integer value node contained in the linked list of non-zero integers .

Input : first line , In numbers 0 The ending non-zero integers are used as the node values of the new linked list ;

The second line , An integer as the value of the deleted node .

Output : Value of each node in the deleted linked list .

Examples : Input 4 2 1 3 3 2 0


Output 4212

#include<stdio.h>#include<stdlib.h>typedef struct list{    int data;    struct list* next;}list;void creatLink(list* head){    list* p = head;    int i;    scanf_s("%d", &i);    while(i!=0)    {        list* new = (list*)malloc(sizeof(list));        new->next = NULL;        new->data = i;        p->next = new;        p = new;        scanf_s("%d", &i);    }}void travelLink(list* head){    list* p = head->next;    while (p != NULL)    {        printf("%d", p->data);        p = p->next;    }}void deleteLink(list* head, int n){    list *curr = head;    list* pre = head->next;    while (pre->data!=0)    {         if (pre->data == n)         {            curr->next = pre->next;            pre = curr->next;         }         else         {             list* q = pre;             curr = q;             pre = q->next;         }    }}int main(){    int n;    list* head = (list*)malloc(sizeof(list));    head->next = NULL;    creatLink(head);// Create a linked list     scanf_s("%d", &n);    deleteLink(head, n);    travelLink(head);// Traversing the linked list     return 0;}
Operation results and error reporting contents

After entering the data , The program didn't respond

My solution ideas and tried methods

Just learn linked list , It's true. I don't know what's wrong

What I want to achieve

Refer to the answer 1:

Hello , I am a little assistant who answers every question , So sorry , This time, you have to answer all your questions , The panel of technical experts timed out and did not answer for you

The number of times to answer all questions deducted from this question , Will answer with questions VIP Experience card (1 A chance to answer every question 、 Shopping malls buy physical books and enjoy 95 A discount ) It will be reissued to your account in the form of .

Because all questions must be answered VIP The experience card is only valid for 1 God , When you need it 【 Direct messages 】 Contact me , I'll reissue it for you .

Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.

Random recommended