Wwerday 2022-04-10 23:59 采纳率: 100%
浏览 49
已结题

单链表的删除,为啥我的代码跑不出来

img


#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int data;
    struct node* next;
}Node;

void CreatList(Node* head, int n){
    Node* p;
    p = (Node*)malloc(sizeof(Node));
    p = head;

    for(int i=0; i<n; i++){
        p->next = (Node*)malloc(sizeof(Node));
        scanf("%d", &p->next->data);
        p = p->next;
    }
    p->next = NULL;
}

void Function(Node* head1,Node* head2, Node* head3){
    Node *pb, *pc;
    int A[200] = {0}, i=0, j=1;
    pb = head2->next;
    pc = head3->next;

    while(pb && pc){
        while((pb->data != pc->data) && pb != NULL && pc != NULL){
            if(pb->data > pc->data){
                pc = pc->next;
            }
            if(pb->data < pc->data){
                pb = pb->next;
            }
        }

        if(pc == NULL || pb == NULL){
            break;
        }
        else{
            while(pb ->data == A[i] || pc->data == A[i]){
                if(pb->data == A[i]){
                    pb = pb->next;
                }
                if(pc->data == A[i]){
                    pc = pc->next;
                }
            }
        }

        A[i+1] = pb->data;
        i++;
        pb = pb->next;
        pc = pc->next;
    }

    Node* pa;
    pa = head1;

    while(pa->next){
        while(pa->next->data < A[j] && pa->next != NULL){
            pa = pa->next;
        }
        if(pa->next->data == A[j] && pa->next !=NULL){
            Node* p = pa->next;
            pa->next = p->next;
            free(p);
        }
        j++;
    }

}

void Print(Node* head1){
    Node* p = head1->next;

    while(p){
        printf("%d ", p->data);
        p = p->next;
    }

}
int main()
{
    Node *head1=NULL,*head2=NULL,*head3=NULL;
    int m,n,p;
    scanf("%d%d%d",&m, &n, &p);
    head1 = (Node*)malloc(sizeof(Node));
    head2 = (Node*)malloc(sizeof(Node));
    head3 = (Node*)malloc(sizeof(Node));
    CreatList(head1,m);
    CreatList(head2,n);
    CreatList(head3,p);
    Function(head1,head2,head3);
    Print(head1);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-04-11 14:45
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node {
        int    data;
        struct node* next;
    }Node;
    void CreatList(Node* head, int n) {
        Node* p;
        //p = (Node*)malloc(sizeof(Node));
        p = head;
        for (int i = 0; i < n; i++) {
            p->next = (Node*)malloc(sizeof(Node));
            scanf("%d", &p->next->data);
            p = p->next;
        }
        p->next = NULL;
    }
    void Function(Node* head1, Node* head2, Node* head3) {
        Node* pb, * pc, * pt, * p;
        pt = p = (Node*)malloc(sizeof(Node));
        p->next = NULL;
        //int A[200] = { 0 }, i = 0, j = 1;
        pb = head2->next;
        pc = head3->next;
        while (pb && pc) {
            if (pb->data > pc->data) {
                pc = pc->next;
            }
            else if (pb->data < pc->data) {
                pb = pb->next;
            }
            else {
                p->next = (Node*)malloc(sizeof(Node));
                p->next->next = NULL;
                p->next->data = pb->data;
                p = p->next;
                pb = pb->next;
                pc = pc->next;
            }
        }
        pb = head1;
        pc = pt;
        while (pb->next && pc->next) {
            if (pb->next->data != pc->next->data) {
                pb = pb->next;
            }
            else if (pb->next->data == pc->next->data) {
                p = pb->next;
                pb->next = p->next;
                free(p);
                pc = pc->next;
            }
        }
        while (pt){
            p = pt;
            pt = pt->next;
            free(p);
        }
        /*    while ((pb->data != pc->data) && pb != NULL && pc != NULL) {
                if (pb->data > pc->data) {
                    pc = pc->next;
                }
                if (pb->data < pc->data) {
                    pb = pb->next;
                }
            }
            if (pc == NULL || pb == NULL) {
                break;
            }
            else {
                while (pb->data == A[i] || pc->data == A[i]) {
                    if (pb->data == A[i]) {
                        pb = pb->next;
                    }
                    if (pc->data == A[i]) {
                        pc = pc->next;
                    }
                }
            }
            A[i + 1] = pb->data;
            i++;
            pb = pb->next;
            pc = pc->next;
        }
        Node* pa;
        pa = head1;
        while (pa->next) {
            while (pa->next->data < A[j] && pa->next != NULL) {
                pa = pa->next;
            }
            if (pa->next->data == A[j] && pa->next != NULL) {
                Node* p = pa->next;
                pa->next = p->next;
                free(p);
            }
            j++;
        }*/
    }
    void Print(Node* head1) {
        Node* p = head1->next;
        while (p) {
            printf("%d ", p->data);
            p = p->next;
        }
        printf("\n");
    }
    int main()
    {
        Node* head1 = NULL, * head2 = NULL, * head3 = NULL;
        int m, n, p;
        scanf("%d%d%d", &m, &n, &p);
        head1 = (Node*)malloc(sizeof(Node)); head1->next = NULL;
        head2 = (Node*)malloc(sizeof(Node)); head2->next = NULL;
        head3 = (Node*)malloc(sizeof(Node)); head3->next = NULL;
        CreatList(head1, m);
        CreatList(head2, n);
        CreatList(head3, p);
        Function(head1, head2, head3);
        Print(head1);
        Print(head2);
        Print(head3);
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题