【问题标题】:linked list program for polynomials exits without giving any output多项式的链表程序退出而不给出任何输出
【发布时间】:2020-11-05 17:01:08
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

struct node{
    int coff;
    int pow;
    struct node* next;
};

struct node* head1 = NULL;
struct node* head2 = NULL;

void insert(int c, int p, struct node* head){
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    struct node* temp = head;
    newnode->coff = c;
    newnode->pow = p;
    newnode->next = NULL;
    
    if(head == NULL){
        head = newnode;
    }else{
        while(temp->next != NULL){
            temp = temp->next;
        }
        temp->next = newnode;
    }
    
}

void display(struct node* h){
    struct node* temp;
    temp = h;
    while(temp != NULL){
        printf("%dx^%d ", temp->coff, temp->pow);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    
    insert(3,2,head1);
    insert(5,1,head1);
    insert(4,0,head1);

    insert(9,2,head2);
    insert(6,1,head2);
    insert(2,0,head2);

    display(head1);
    display(head2);
    
    return 0;
}

我已经为我的链表创建了节点结构。然后我为插入和显示多项式创建了两个函数。我为两个不同多项式的存储地址创建了 head1 和 head2。我想在插入和显示函数中使用 head1 和 head2 作为参数。然后最后我想打印两个多项式。但是有一个瓶颈。当我执行我的程序时,它会退出而不给出任何输出。

我的预期输出是:

3x^2 5x^1 2x^0

9x^2 6x^1 4x^0

我该如何解决这个问题?函数中的 (struct node* head) 参数有什么问题吗?为什么这个程序没有输出就退出了?

非常感谢!

【问题讨论】:

  • 我认为你的意思是 9x^2 5x^1 4x^0 用于第二个输出
  • @AbhayAravinda 是的。这是我的错误。对于那个很抱歉。我将对其进行编辑。
  • 关于:struct node* newnode = (struct node*)malloc(sizeof(struct node)); 1) 在 C 中,返回的类型是 void*,可以分配给任何指针。强制转换只会使代码混乱,使其更难以理解、调试等。 2) 始终检查 (!=NULL) 返回值以确保操作成功。如果不成功(==NULL),则调用perror( "malloc failed" );,这样系统认为发生错误的错误消息和文本原因都会输出到stderr

标签: c function linked-list arguments polynomials


【解决方案1】:

您的head = newnode; 行实际上并没有改变head1head2,因为指针本身是按值传递的。这导致head1head2 剩余NULL,从而导致没有输出。您可以通过将指向头节点的指针传递给insert 来解决此问题。快速修改您的代码以实现此功能:

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

struct node{
    int coff;
    int pow;
    struct node* next;
};

struct node* head1 = NULL;
struct node* head2 = NULL;

void insert(int c, int p, struct node **head){
    struct node* newnode = (struct node*)malloc(sizeof(struct node));
    struct node* temp = *head;
    newnode->coff = c;
    newnode->pow = p;
    newnode->next = NULL;
    
    if(*head == NULL){
        *head = newnode;
    }else{
        while(temp->next != NULL){
            temp = temp->next;
        }
        temp->next = newnode;
    }
    
}

void display(struct node* h){
    struct node* temp;
    temp = h;
    while(temp != NULL){
        printf("%dx^%d ", temp->coff, temp->pow);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    
    insert(3, 2, &head1);
    insert(5, 1, &head1);
    insert(4, 0, &head1);

    insert(9, 2, &head2);
    insert(6, 1, &head2);
    insert(2, 0, &head2);

    display(head1);
    display(head2);
    
    return 0;
}

【讨论】:

    【解决方案2】:

    看看 void insert(int c, int p, struct node* head)

    因为 'head' 是按值传递的(所有 C 参数都这样做), 此函数的调用者没有机会获得“内部修改”head。 因此,在您的情况下,main() 中,head1 始终为 NULL。

    我对你的代码做了一些修改,看起来可行:) 但是不要忘记free所有分配的节点。

    include <stdio.h>
    #include <stdlib.h>
    
    struct node{
        int coff;
        int pow;
        struct node* next;
    };
    
    struct node* head1 = NULL;
    struct node* head2 = NULL;
    
    struct node* insert(int c, int p, struct node* head){
        struct node* newnode = (struct node*)malloc(sizeof(struct node));
        struct node* temp = head;
        newnode->coff = c;
        newnode->pow = p;
        newnode->next = NULL;
        
        if(head == NULL){
            head = newnode;
        }else{
            while(temp->next != NULL){
                temp = temp->next;
            }
            temp->next = newnode;
        }
        
        return head;
    }
    
    void display(struct node* h){
        struct node* temp;
        temp = h;
        while(temp != NULL){
            printf("%dx^%d ", temp->coff, temp->pow);
            temp = temp->next;
        }
        printf("\n");
    }
    
    int main(){
        
        head1 = insert(3,2,head1);
        insert(5,1,head1);
        insert(4,0,head1);
    
        head2 = insert(9,2,head2);
        insert(6,1,head2);
        insert(2,0,head2);
    
        display(head1);
        display(head2);
        
        return 0;
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 2015-10-28
      相关资源
      最近更新 更多