ChanQi16
2017-03-27 10:56
采纳率: 100%
浏览 10.6k
已采纳

引发了未经处理的异常:读取访问权限冲突

请大神指教一下,调试显示 q 是nullptr,谢谢了

 # include <malloc.h>
# include <stdlib.h>//exit
# include <stdio.h>

typedef struct Node
{
    int data;
    struct Node *next;
}Node, *LinkList;

LinkList Create_List(LinkList &L)
{
    Node *p, *q;
    int i, n;
    int data;
    L = (LinkList)malloc(sizeof(Node));
    q = L;
    printf("链表的长度为:");
    scanf_s("%d", &n);
    for (i = 0; i<n; i++)
    {
        scanf_s("%d", &data);
        p = (struct Node*)malloc(sizeof(struct Node));
        p->data = data;
        q->next = p;
        q = p;
    }
    q->next = NULL;
    return L;
}

LinkList Merge_List(LinkList La, LinkList Lb)
{
    Node *p, *q;
    LinkList L;
    L = (LinkList)malloc(sizeof(Node));
    L = NULL;
    p = La->next;
    q = Lb->next;
    while (La&&Lb)
    {
        if (La->data<Lb->data)
        {
            L = p;
            p = p->next;
        }
        else
        {
            L = q;
            q = q->next;
        }
    }
    L = p ? p : q;
    return L;
}

LinkList Reverse_List(LinkList L)
{
    Node *p, *q;
    p = L->next;
    L->next = NULL;
    while (p != NULL)
    {
        q = p->next;
        p->next = L->next;
        L->next = p;
        p = q;
    }
    return L;
}


void Print_List(LinkList L)
{
    Node *p = L->next;
    while (p != NULL)
    {
        printf("%d", p->data);
        p = p->next;
    }
    printf("\n");
}


int main()
{
    LinkList La, Lb;
    La = (LinkList)malloc(sizeof(Node));
    Lb = (LinkList)malloc(sizeof(Node));
    Create_List(La);
    Create_List(Lb);
    Print_List(Merge_List(La, Lb));
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • -Leto- 2017-03-27 13:41
    最佳回答
    # include <malloc.h>
    # include <stdlib.h>//exit
    # include <stdio.h>
    
    typedef struct Node
    {
        int data;
        struct Node *next;
    }Node, *LinkList;
    
    LinkList Create_List(LinkList &L)
    {
        Node *q;
        int i, n;
        int data;
        L = (LinkList)malloc(sizeof(Node));
        q = L;
        printf("链表的长度为:");
        scanf_s("%d", &n);
        for (i = 0; i<n; i++)
        {
            scanf_s("%d", &data);
            q->data = data;
    
            q->next = (LinkList)malloc(sizeof(struct Node));
    
            if (i < n - 1)
                q = q->next;
        }
        q->next = 0;
        return L;
    }
    
    
    LinkList Merge_List(LinkList La, LinkList Lb)
    {
        LinkList p, t;
        LinkList a = La, b = Lb;
        if (La->data < Lb->data) {
            t = p = La;
            a = La->next;
        }
        else {
            t = p = Lb;
            b = Lb->next;
        }
        while (a!=NULL&&b!=NULL) {
            if (a->data < b->data) {
                t->next = a;
                a = a->next;
                t = t->next;
            }
            else {
                t->next = b;
                b = b->next;
                t = t->next;
            }
        }
        if (a == NULL) {
            t->next = b;
        }
        else {
            t->next = a;
        }
        return p;
    }
    
    LinkList Reverse_List(LinkList L)
    {
        Node *p, *q;
        p = L->next;
        L->next = NULL;
        while (p != NULL)
        {
            q = p->next;
            p->next = L->next;
            L->next = p;
            p = q;
        }
        return L;
    }
    
    
    void Print_List(LinkList L)
    {
        while (L != NULL)
        {
            printf("%d ", L->data);
            L = L->next;
        }
        printf("\n");
    }
    
    
    int main()
    {
        LinkList La, Lb;
        La = (LinkList)malloc(sizeof(Node));
        Lb = (LinkList)malloc(sizeof(Node));
        Create_List(La);
        Create_List(Lb);
        Print_List(Merge_List(La, Lb));
    
        getchar();
        getchar();
    }
    
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题