首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

想不通

2012-02-07 
想不通,请教大家#includestdio.h#includestdlib.h#includemalloc.h#includewindef.htypedefstruct

想不通,请教大家
#include   <stdio.h>
#include   <stdlib.h>
#include   <malloc.h>
#include   <windef.h>

typedef   struct   node
{
int   iData;
struct   node   *pNext;
}Node;

Node   *NodeCreate(void);
BOOL   NodeInsert(Node   *head,   Node   *p,   int   data);
Node   *NodeFind(Node   *head,   int   data);
BOOL   NodeDelete(Node   *head,   Node   *p);

int   main(int   argc,   char   **argv)
{
int   i   =   100;
Node   *head;
head   =   NodeCreate();
NodeInsert(head,   head,   i);
printf( "%d   \n ",   head-> pNext-> iData);
Node   *find;
find   =   NodeFind(head,   100);
printf( "%x   \n ",   find);
NodeDelete(head,   find);
printf( "%x   \n ",   find);//问题是,我已经删除了此节点,为何打印find     的地址与删除之前一样而不是空地址呢?

return   0;
}

Node   *NodeCreate(void)
{
Node   *pHead;
pHead   =   (Node   *)malloc(sizeof(Node));
if(pHead   ==   NULL)
return   NULL;
else
{
pHead-> iData   =   0;
pHead-> pNext   =   NULL;
}
return   pHead;
}

BOOL   NodeInsert(Node   *head,   Node   *p,   int   data)//p   is   the   last   point   of   insertion
{
if(head   ==   NULL)
{
printf( "The   chain   diagram   is   empty! ");
return   false;
}
Node   *q;
q   =   (Node   *)malloc(sizeof(Node));
if(q   ==   NULL)
{
printf( "There   is   not   enough   memory! ");
return   false;
}
else
{
q-> iData   =   data;
q-> pNext   =   p-> pNext;
        p-> pNext   =   q;
}

return   true;
}

Node   *NodeFind(Node   *head,   int   data)
{
if(head-> pNext   ==   NULL)
{
printf( "The   chain   diagram   is   empty! ");
return   false;
}
Node   *p;
p   =   head-> pNext;
while(p-> iData   !=   data)
p   =   p-> pNext;
if(p   ==   NULL)
{
printf( "The   value   you   need   does   not   exist! ");
return   NULL;
}
return   p;
}

BOOL   NodeDelete(Node   *head,   Node   *p)//p   can   not   be   the   head   point
{
if(head-> pNext   ==   NULL)
{
printf( "The   chain   diagram   is   empty! ");
return   false;
}
if(p   ==   NULL   ||   p   ==   head)
return   false;
//first   we   need   to   find   the   last   point   of   p
Node   *q;
q   =   head;
while(q-> pNext   !=   p)
q   =   q-> pNext;
q-> pNext   =   p-> pNext;
free(p);
return   true;
}

------解决方案--------------------


删除了只是代表别人可以用这块内存了,并不代表这块内存必须是0000,因为没有必要啊。
别人要用这块内存的时候肯定是要重新覆盖的,所以原先的数据不管是什么都无所谓,这样就提高了效率。
不过你确实看不惯这种方式,可以手动在NodeDelete里给赋值成0000
[解决办法]
bool NodeDelete(Node *head, Node **p);

NodeDelete(head, &find);

bool NodeDelete(Node *head, Node **p)//p can not be the head point
{
if(head-> pNext == NULL)
{
printf( "The chain diagram is empty! ");
return false;
}
if(*p == NULL || *p == head)
return false;
//first we need to find the last point of p
Node *q;
q = head;
while(q-> pNext != *p)
q = q-> pNext;
q-> pNext = (*p)-> pNext;
free(*p);
*p = NULL;
return true;
}

函数传指向指针地址的指针!
[解决办法]
free()释放了你申请的那块内存.但是指针却成了 "野指针 "了。
最好在释放后立即将该指针指向NULL
[解决办法]
理论上说,指针释放之后,应该指向的是随机的地址,但是因为并未对它重新赋值,所以它还指向原来的内存单元,但是这个时候这块内存已经不归你使用了,所以最好是把指针重新赋值为空,以免你程序中不小心用到时出错。

热点排行