Many people have different interpretations, and the problem I have is that the open memory space is less than the memory space of the operation. In other words, my free memory is out of bounds.
This is the code that I created in my list structure:
1 Pnode Create () {2 intLen//Total count of nodes to be created.3 inti;4 intVal//Temp value for the current node.5printf"Enter the size of nodes:");6scanf"%d", &len);7Pnode Phead = (pnode) malloc (sizeof(Pnode));8Phead->pnext =NULL;9Pnode Ptail =Phead;Ten One if(NULL = =phead) { Aprintf"Allocate Memory failed!"); -Exit0); - } the for(i =0; i < Len; i++) - { -Pnode pcur = (pnode) malloc (sizeof(Pnode)); - if(NULL = =pcur) { +printf"Allocate Memory failed!"); -Exit0); + } Aprintf"Enter the%d-th value:", i +1); atscanf"%d", &val); -Pcur->data =Val; - - //set the new node as the tail node. -Ptail->pnext =pcur; -Pcur->pnext =NULL; inPtail =pcur; - } to returnPhead; +}
This is how I define the structure:
1 struct Node {2 int data; 3 struct node * pnext; 4 } NODE, * pnode;
When deleting an element (the code for the error) is:
1 BOOLDelete (Pnode Phead,intPosint*v) {2 inti =-1;3Pnode Pnode =Phead;4 while((I < pos-1) && Pnode! =NULL) {5Pnode = pnode->Pnext;6i++;7 }8 if(Pos < I | | pnode = =NULL)9 return false;TenPnode ptmp = pnode->pnext;//store to free later. One*v = ptmp->data; APnode->pnext = pnode->pnext->Pnext; - Free (ptmp); -Ptmp =NULL; the return true; -}
I've been debugging this code over and over again, and I've found that all the pointer fields and data fields of the nodes are what I'm looking for. It was in the free time that I called it wrong.
Originally I opened up memory, the size of the designation is wrong, should be put:
1 pnode pnew = (pnode) malloc (sizeof(pnode));
Switch
1 pnode pnew = (pnode) malloc (sizeof(NODE));
The size of the opening node should be the size of the structure, in fact, I ' insert ' The new node, this line of code is wrong, but will not error. I think there should be no release.