ZBLOG

#include #include typedef struct node { double X; int Z; struct node* next; } Node; int InitNode(Node** head) { *head = (Node*)malloc(sizeof(Node)); if (*head == NULL) return 0; (*head)->next = NULL; (*head)->X=0.0; ...

根据您提供的代码,我注意到在创建链表时可能会导致超时的原因。具体来说,在插入节点时使用了比较复杂的逻辑,导致了链表的遍历操作。

为了解决这个问题,可以尝试以下优化:

  1. 将链表节点的插入逻辑简化:可以通过设置一个临时指针 q 来记录上一个节点,并将新节点插入到其后面。

  2. 修改排序逻辑:可以先按照 Z 的大小进行排序,然后再合并相同 Z 值的节点。

下面是修改后的代码示例:

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

typedef struct node {
    double X;
    int Z;
    struct node* next;
} Node;

int InitNode(Node** head) {
    *head = (Node*)malloc(sizeof(Node));
    if (*head == NULL)
        return 0;
    
    (*head)->next = NULL;
    (*head)->X = 0.0;
    (*head)->Z = 0;
    
    return 1;
}

void creat(Node* head) {
    Node* r = head, *s;  
    int a, b;

    while (1) {
        s = (Node*)malloc(sizeof(Node));
        scanf("%d,%d", &a, &b);

        if (a == 0 && b == 0)
            break;

        r = head->next;
        
        s->X = a;
        s->Z = b;

        // 插入节点
        while (r != NULL && r->Z > s->Z) {
            r = r->next;
        }
        
        if (r != NULL && r->Z == s->Z) {
            r->X += s->X;
            free(s);
        } else {
            s->next = r;
            head->next = s;
        }
    }
}

void output(Node* head) {
    Node* p = head->next;

    printf("f(X)=");
    
    if (p == NULL)
        printf("0.0");
    
    int count = 0;
    
    while (p != NULL) {
        if (p->X != 0.0) {
            if(count==0){
                if(p->Z==0)
                    printf("%.1f", p->X);
                else if(p->Z==1 && p->X==1.0)
                    printf("X");
                else if(p->Z==1)
                    printf("%.1fX", p->X);
                else
                    printf("%.1fX^%d", p->X, p->Z);

                count=1;
            }else{
                if(p->X>0)
                    printf("+");
                else{
                    printf("-");
                    p->X *= -1;
                }

                if(p->X==1 && p->Z==0)
                    printf("%.1f", p-X);
                else if(p-X=1 && p-Z=1)
                    printf("X");
                else if(p-X=1 && p-Z!=0 && p-Z!= 11 )
                    printnf("x^%d",p-Z);
               // .....
           }
       }

       p = p -> next ;
   }
   
   printf("\n");
}

void freeList(Node* head){
    Node* current = head;

    while(current != NULL){
        Node* nextNode = current -> next ;
        
        free(current);
        current = nextNode;
    }

    head = NULL;
}

int main() {
    Node* head;

    if (!InitNode(&head)) {
        printf("Initialization failed.\n");
        return 0;
    }
    
    creat(head);
    
    output(head);
    
    freeList(head);
    
    return 0;
}

通过上述修改,可以简化节点插入逻辑,并避免了频繁的链表遍历操作,从而提高程序的执行效率,减少超时问题。希望这能对您有所帮助!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=8729

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?