Home > Back-end >  For free ()
For free ()

Time:10-04

# include "stdio.h"
# include "stdlib. H"

Typedef struct Node {
int a;
Struct Node * next;
} LINKLIST;

Int GetElem (LINKLIST * list, the const int I) {
Int j=1;
LINKLIST * p=list;
While (p & amp; & J & lt; I) {
P=p - & gt; Next;
+ + j;
}
if (! P) {
Printf (" ERROR: beyond the scope of ");
exit(1);
};
The return p - & gt; a;
}

Void DeletElem (LINKLIST * list, the const int I) {
Int j=1;
LINKLIST * p=list, * p_up=NULL;
While (p & amp; & J & lt; I) {
P_up=p;
P=p - & gt; Next;
j++;
}
if (! P) {
Printf (" ERROR: beyond the scope of ");
exit(1);
}
P_up - & gt; Next=p - & gt; Next;
P - & gt; Next=NULL;
//free (p);
//p=NULL;
}

Int main () {
LINKLIST x, y, z, * the head;
X.a=1;
Y.a=2;
Z.a=3;
The head=& amp; x;
X.n ext=& amp; y;
Y.n ext=& amp; Z;
Z.n ext=NULL;
DeletElem (head, 2);
Printf (" % d, % d \ n ", GetElem (head, 1), GetElem (head, 2));

Why delete list elements after an error free there?

CodePudding user response:

Problem is to remove the node is not your application memory, is pointing in the direction of the main function of z variables, z is not free, because the memory z is program automatically assigned, not for themselves), only their own malloc application memory needs to be free,
  • Related