cdsn_Python 2023-03-13 13:10 采纳率: 69%
浏览 19
已结题

关于#链表#的问题,如何解决?

新学链表
题目:已知有两个递增的正整数序列A和B,序列中元素个数未知,同一序列中不会有重复元素出现,有可能某个序列为空。现在将AB所有的元素归并到一个链表,A和B相同的元素放到另一个链表里。

typedef struct node
{
    int number;
    struct node* nextPtr;
}Node;
int input(Node* head)
{
    int n,cnt=0;
    scanf("%d",&n);
    struct node* buf=head;
    while (n!=-1)
    {
        struct node* temp=(struct node*)malloc(sizeof(struct node*));
        temp->number=n;
        temp->nextPtr=NULL;
        buf->nextPtr=temp;
        buf=buf->nextPtr;
        scanf("%d",&n);
        cnt+=1;
    }
    return cnt;
}

void output(Node* head)
{
    Node* tempa=head->nextPtr;
    while (tempa!=NULL)
    {
        printf("%d",tempa->number);
        tempa=tempa->nextPtr;
        
    }
}

int main()
{
    Node* headA=(Node*)malloc(sizeof(Node*));
    Node* headB=(Node*)malloc(sizeof(Node*));
    int nA=input(headA),nB=input(headB);
    Node* temp1=headA->nextPtr,*temp2=headB->nextPtr;
    Node* ans1=(Node*)malloc(sizeof(Node*)),*ans2=(Node*)malloc(sizeof(Node*));
    Node* tick1=ans1,*tick2=ans2;
    while (temp1!=NULL&&temp2!=NULL)
    {
        if (temp1->number==temp2->number)
        {
            Node *buf=(Node*)malloc(sizeof(Node*));
            buf->number=temp2->number;
            buf->nextPtr=NULL;
            tick2->nextPtr=buf;
            tick2=tick2->nextPtr;
            temp1=temp1->nextPtr;
            temp2=temp2->nextPtr;
            
        }
        else if (temp1->number>temp2->number)
        {
            Node *buf=(Node*)malloc(sizeof(Node*));
            buf->number=temp2->number;
            buf->nextPtr=NULL;
            tick1->nextPtr=buf;
            tick1=tick1->nextPtr;
            temp2=temp2->nextPtr;
            
        }
        else
        {
            Node *buf=(Node*)malloc(sizeof(Node*));
            buf->number=temp1->number;
            buf->nextPtr=NULL;
            tick1->nextPtr=buf;
            tick1=tick1->nextPtr;
            
            temp1=temp1->nextPtr;
        }
        if (temp1==NULL)
        {
            
            while (temp2!=NULL)
            {
                Node *buf=(Node*)malloc(sizeof(Node*));
                buf->number=temp2->number;
                buf->nextPtr=NULL;
                tick1->nextPtr=buf;
                tick1=tick1->nextPtr;
                temp2=temp2->nextPtr;
                
            }
        }
        if (temp2==NULL)
        {
            while (temp1!=NULL)
            {
                Node *buf=(Node*)malloc(sizeof(Node*));
                buf->number=temp1->number;
                buf->nextPtr=NULL;
                tick1->nextPtr=buf;
                tick1=tick1->nextPtr;
                temp1=temp1->nextPtr;
            }
        }
    }
    output(ans1);
    output(ans2);
}

现在问题是运行不出答案,代码比较复杂所以自己没有找到原因,希望能帮忙解答一下
用例:1 3 4 5 6 7 -1
2 3 6 8 9 10 11-1

输出:1 2 3 4 5 6 7 8 9 10 11
3 6

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2023-03-13 13:17
    关注

    这要仔细看看,调试一下

     struct node* temp=(struct node*)malloc(sizeof(struct node*));
    改为
     struct node* temp=(struct node*)malloc(sizeof(struct node));
    
    
    

    后面所有的malloc都是错的,sizeof后面不能是指针类型的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月21日
  • 已采纳回答 3月13日
  • 创建了问题 3月13日

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。