标题:在调试过程中出现“123.exe 中的 0x01161677 处最可能的异常: 0xC0000005: ...
取消只看楼主
wuzi2010ED
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-6-6
结帖率:100%
已结贴  问题点数:20 回复次数:0 
在调试过程中出现“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 编辑 ]
搜索更多相关主题的帖子: include 元素 head 
2012-06-06 10:47



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-370908-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.064233 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved