current position:Home>The linked list code in cFree can run, but the memory limit is exceeded when submitting

The linked list code in cFree can run, but the memory limit is exceeded when submitting

2022-02-02 22:22:59 CSDN Q & A

The phenomenon and background of the problem

Find out cfree The linked list code can run , However, a memory error was found when submitting to the platform , Help! !
requirement :
Enter a series of characters to -# As an end sign , Use tail interpolation to create linked list and store data , Then delete a new character in the specified position , And output the result

On the first line, enter a string , With # end
An integer p
Linked list all data ( Before deleting 、 After deleting ) One line each

The sample input
Sample output
a b c d e
a c d e

#include"stdio.h"#include"stdlib.h"typedef struct node{    char data;    struct node *next;}Node;Node * creatlink();void Dellink(Node *h,int p);void output(Node *h);void destroylink(Node *h);main(){    Node *head=NULL;    int p;    head = creatlink();     scanf("%d",&p);    output(head);                      Dellink(head,p);                     output(head);                      destroylink(head);                  }Node * creatlink(){   Node *head=NULL,*p,*q;   char ch;      head=(Node *)malloc(sizeof(Node));  // Create a header node , Do not store valid data     head->next=NULL;   q=head;   while(1)        // Create a linked list    {           scanf("%c",&ch);           if(ch!='#'){               p = (struct node*)malloc(sizeof(struct node));               p -> data = ch;               p -> next = NULL;               q -> next = p;               q = p;           }           else           break;   }   return head;}void Dellink(Node *h,int p){    Node *pfront,*pafter,*q;    pfront=h;    pafter=h;    int i;    for(i=0;i<p-1;i++){        q = pfront->next;        pfront = q;        }        for(i=0;i<p+1;i++){        q = pafter->next;        pafter = q;        }        pfront->next = pafter;         }void output(Node *h){    Node *p ;    p=h->next;    while(p){        printf("%c ",p->data);        p=p->next;    }    printf("\n");}void destroylink(Node *h){    Node *q;    while(h!=NULL)    {        q=h;        h=h->next;        free(q);    }}
Operation results and error reporting contents

cfree: nothing
campus OJ platform :Runtime Error:Segmentation fault

My solution ideas and tried methods
What I want to achieve

Refer to the answer 1:

This is not a memory overrun , This is a mistake
q = pafter->next;
pafter = q;
This is risky , What if the last node is deleted , It is not after ah , the last one pafter->next It will break down . You should check pafter Is it empty . If it is empty, it means that the last node to be deleted , Directly to pfront->next = NULL Okay .
in addition , For deleted nodes, you should also free It's in line with the style of your code

Refer to the answer 2:

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

Random recommended