在调试过程中出现“123.exe 中的 0x01161677 处最可能的异常: 0xC0000005: 写入位置 0x00000000 时发生访问冲突”的问题怎
下面是我编写的一段代码,请帮忙指点一下,我实在调不出来了。//检查一个(单向)链表,删除其中数据大于100的元素
#include <stdio.h>
#include <stdlib.h>
struct Link *AppendNode(int data,struct Link *head);
struct Link *DeleteNode(int data,struct Link *head);
void DispLink(struct Link *head);
void DeleteMemory(struct Link *head);
struct Link
{
int data;
struct Link *next;
};
main()
{
int data;
struct Link *head = NULL, *p, *pr = NULL;
printf("Please input a node data:");
scanf("%d",&data);
while (data >= 0)
{
head = AppendNode(data,head);
printf("Please input next node data:");
scanf("%d",&data);
}
printf("Creating a link is succeeded.\n\n");
printf("Before Deleting:\n\n");
DispLink(head);
p = head;
while (p != NULL)
{
while (p->data <= 100)
{
pr = p;
p = p->next;
}
head = DeleteNode(p->data,head);
}
printf("After Deleting:\n\n");
DispLink(head);
DeleteMemory(head);
}
struct Link *AppendNode(int data,struct Link *head)
{
struct Link *p = NULL,*pr = head;
p = (struct Link *)malloc(sizeof(struct Link));
if (p == NULL)
{
printf("No ennough memory to alloc!");
exit(0);
}
if (head == NULL)
{
head = p;
}
else
{
while (pr->next != NULL)
{
pr = pr->next;
}
pr->next = p;
}
pr = p;
pr->data = data;
pr->next = NULL;
return head;
}
struct Link *DeleteNode(int data,struct Link *head)
{
struct Link *p = head, *pr = head;
if (head == NULL)
{
printf("No Link Table!");
return head;
}
while (data != p->data && p->next != NULL)
{
pr = p;
p = p->next;
}
if (data == p->data)
{
if (p == head)
{
head = p->next;
}
else
{
pr->next = p->next;
}
free(p);
}
else
{
printf("Did not find the Node.");
}
return head;
}
void DispLink(struct Link *head)
{
struct Link *p = head;
int j = 1;
while (p != NULL)
{
printf("%5d%10d\n",j,p->data);
p = p->next;
j++;
}
}
void DeleteMemory(struct Link *head)
{
struct Link *p = head, *pr = NULL;
while (p != NULL)
{
pr = p;
p = p->next;
free (pr);
}
}
万分感谢各位大神的帮忙!!
[ 本帖最后由 wuzi2010ED 于 2012-6-6 10:53 编辑 ]