【问题标题】:Creating a polynomial with a linked list in C在 C 中使用链表创建多项式
【发布时间】:2013-11-16 16:17:35
【问题描述】:

我正在使用 C 中的链表创建多项式,但遇到了问题。有人可以帮我写代码吗?在create函数中,我刚刚创建了一个节点,我想把节点放在函数insert的正确位置,然后我想返回多项式p1

我也无法理解return 语句将如何工作。请告诉我代码中的错误与我的方法有关。

struct node
{
    int cof;
    int exp;
    struct node *link;
};
struct node * create(struct node *q)
 {
  int i,n;
  printf("enter the number of nodes");
  scanf("%d",&n);
  struct node *ptr=(struct node *)malloc (sizeof(struct node));
  for(i=0;i<n;i++)
  {
                  printf("entre the coefficient and exponent respectivly");
                  scanf("%d%d",&ptr->cof,&ptr->exp);
                  ptr->link=NULL;
                  q=insert(ptr,q);
  }
  return q;
 }
struct node * insert(struct node *ptr,struct node *p)
 {
  struct node *temp,*b;
  if(p==NULL)
  p=ptr;
  else
  {
      if((p->exp)<(ptr->exp))
      {
                              ptr->link=p;
                              p=ptr;
      }
      else
      {
          temp=p;
          while((temp!=NULL)||((temp->link->exp)<(ptr->exp)))
          temp=temp->link;
          b=temp->link;
          temp->link=ptr;
          ptr->link=b;
      }
  }
  return p;
  }
void display(struct node *ptr)
 {
   struct node *temp;
  temp=ptr;
  while(temp!=NULL)
  {
                   printf("%d x ^ %d + ",temp->cof,temp->exp);
                   temp=temp->link;
  }
 }

 int main()
  {
  printf("enter the first polynomial");
  struct node *p1=NULL,*p2=NULL; 
  p1=(struct node *)malloc(sizeof(struct node));
  p2=(struct node *)malloc(sizeof(struct node));

  p1=create(p1);

  printf("entr secon dpolynimial");
  create(p2);

  display(p1);
  display(p2);

  getch();
  return 0;
 }

【问题讨论】:

  • 问题1:你要插入多少个节点?问题2:你分配了多少?

标签: c linked-list return-value polynomial-math


【解决方案1】:

此代码至少适用于某些输入:

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

struct node
{
    int cof;
    int exp;
    struct node *link;
};

struct node *create(struct node *q);
struct node *insert(struct node *ptr, struct node *p);
void display(char const *tag, struct node *ptr);
void err_exit(char const *tag);

struct node *create(struct node *q)
{
    int i, n;
    printf("enter the number of nodes: ");
    if (scanf("%d", &n) != 1)
        err_exit("Read error (number of nodes)");
    for (i = 0; i < n; i++)
    {
        struct node *ptr = (struct node *)malloc(sizeof(struct node));
        if (ptr == 0)
            err_exit("Out of memory (1)");
        printf("enter the coefficient and exponent respectively: ");
        if (scanf("%d%d", &ptr->cof, &ptr->exp) != 2)
            err_exit("Read error (coefficient and exponent)");
        ptr->link = NULL;
        q = insert(ptr, q);
        display("after input", q);
    }
    return q;
}

struct node *insert(struct node *ptr, struct node *p)
{
    struct node *temp, *b;
    if (p == NULL)
        p = ptr;
    else
    {
        display("insert: p   = ", p);
        display("insert: ptr = ", ptr);
        if (p->exp < ptr->exp)
        {
            ptr->link = p;
            p = ptr;
        }
        else
        {
            temp = p;
            while ((temp->link != NULL) && (temp->link->exp < ptr->exp))
                display("insert: tmp = ", temp),
                temp = temp->link;
            display("insert: post loop", temp);
            b = temp->link;
            temp->link = ptr;
            ptr->link = b;
        }
    }
    return p;
}

void display(char const *tag, struct node *ptr)
{
    struct node *temp;
    const char *pad = "";
    temp = ptr;
    printf("%s: ", tag);
    while (temp != NULL)
    {
        printf("%s%d x ^ %d", pad, temp->cof, temp->exp);
        temp = temp->link;
        pad = " + ";
    }
    putchar('\n');
}

int main(void)
{
    printf("enter the first polynomial:\n");
    struct node *p1 = NULL, *p2 = NULL;

    p1 = create(p1);

    printf("enter the second polynomial:\n");
    p2 = create(p2);

    display("p1", p1);
    display("p2", p2);

    return 0;
}

void err_exit(char const *tag)
{
    fprintf(stderr, "%s\n", tag);
    exit(1);
}

修复包括:

  • 测试 I/O 错误
  • 添加标签显示功能
  • 大量使用显示功能
  • 添加错误退出功能并使用
  • 主要修复:在 while 循环中正确处理 insert() 中的测试:
    • 测试temp-&gt;link 是否为空
    • 在测试有效性时使用&amp;&amp; 而不是||
  • 改进display() 中的打印(仅在分隔两个术语时输出+;在末尾输出换行符)
  • 不要在main() 中泄漏内存。
  • 不要将未初始化的内存传递给create()
  • 不要忽略来自create() 的返回。

示例运行:

enter the first polynomial:
enter the number of nodes: 3
enter the coefficient and exponent respectively: 2 2
after input: 2 x ^ 2
enter the coefficient and exponent respectively: 3 1
insert: p   = : 2 x ^ 2
insert: ptr = : 3 x ^ 1
insert: post loop: 2 x ^ 2
after input: 2 x ^ 2 + 3 x ^ 1
enter the coefficient and exponent respectively: 4 0
insert: p   = : 2 x ^ 2 + 3 x ^ 1
insert: ptr = : 4 x ^ 0
insert: post loop: 2 x ^ 2 + 3 x ^ 1
after input: 2 x ^ 2 + 4 x ^ 0 + 3 x ^ 1
enter the second polynomial:
enter the number of nodes: 5
enter the coefficient and exponent respectively: 1 0
after input: 1 x ^ 0
enter the coefficient and exponent respectively: 2 1
insert: p   = : 1 x ^ 0
insert: ptr = : 2 x ^ 1
after input: 2 x ^ 1 + 1 x ^ 0
enter the coefficient and exponent respectively: 4 6
insert: p   = : 2 x ^ 1 + 1 x ^ 0
insert: ptr = : 4 x ^ 6
after input: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0
enter the coefficient and exponent respectively: 3 2
insert: p   = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0
insert: ptr = : 3 x ^ 2
insert: tmp = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0
insert: tmp = : 2 x ^ 1 + 1 x ^ 0
insert: post loop: 1 x ^ 0
after input: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
enter the coefficient and exponent respectively: 9 3
insert: p   = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
insert: ptr = : 9 x ^ 3
insert: tmp = : 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
insert: tmp = : 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2
insert: tmp = : 1 x ^ 0 + 3 x ^ 2
insert: post loop: 3 x ^ 2
after input: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2 + 9 x ^ 3
p1: 2 x ^ 2 + 4 x ^ 0 + 3 x ^ 1
p2: 4 x ^ 6 + 2 x ^ 1 + 1 x ^ 0 + 3 x ^ 2 + 9 x ^ 3

有空间认为按指数排序不能正常工作,但代码不会崩溃。使用valgrind 运行不会发现内存访问错误;不过,它就像众所周知的筛子一样泄漏。

【讨论】:

    【解决方案2】:

    希望对您有所帮助。不过,您需要减少一些冗余。

    #include <iostream>
    using namespace std;
    typedef struct node
    {
        int coef,exp;
        node* next;
    };
    node* start;
    node* start1;
    node* start2;
    node* beg=NULL;
    node*begmer=NULL;
    void newpol(int);
    void display(); 
    void merger();
    node * allocate(int n)
    {
        node* new_node=new node;
    cout << "\n Enter the coefficient:";
    cin>> new_node->coef;
    new_node->exp=n;
    new_node->next=NULL;
    if(start==NULL)
        {
            start=new_node;
            beg=start;
        }
        else
        {
            start->next=new_node;
        }
        return new_node;
    }
    int main()
    {
    
        char c='y';
        do
        {
            int ch;
            cout<< "\n Enter your choice: \n 1. Create new expression \n 2. Merge two expression";
            cin >>ch;
            switch(ch)
            {
            case 1:
                int n;
                cout<< "\n Enter the order of polynomial: ";
                cin>>n;
                newpol(n);
                display();
                break;
            case 2:
                merger();
                beg=begmer;
                cout<<"\n After merging::\t";
                display();
                break;}
            cout<< "\n more??";
            cin>>c;
        }while(c!='n');
    }
    void newpol(int n)
    {start=NULL;
        int c=n;
        cout<<"\n for order "<<n<<" polynomial:";
        while(c>=0)
        {
            start=allocate(c);
            c--;
        }
    }
    void display()
    {
        node* temp=new node;
        temp=beg;
        cout<<"\n";
        do{
            cout <<"+
    
     "<<temp->coef<<" X^ "<<temp->exp;
            temp=temp->next;
        }while (temp!=NULL);
    }
    void merger()
    {
            node* startmerge=NULL;
        int n1,n2;
        cout<<"degree of first and sec:";
        cin >>n1>>n2;
        newpol(n1);
        start1=beg;
        display();
        newpol(n2);
        start2=beg;
        display();
        do{
        node* new_node=new node;
        if (start1->exp==start2->exp)
        {
            new_node->exp=start1->exp;
            new_node->coef=start1->coef+start2->coef;
            start1=start1->next;
            start2=start2->next;
        }
        else if (start1->exp>start2->exp)
        {
            new_node->exp=start1->exp;
            new_node->coef=start1->coef;
            start1=start1->next;
        }
        else if (start1->exp<start2->exp)
        {
            new_node->exp=start2->exp;
            new_node->coef=start2->coef;
            start1=start2->next;
        }
         if (startmerge==NULL)
                        {
                            startmerge=new_node;
                            begmer=startmerge;
                        }
                        else{
                            startmerge->next=new_node;
                            startmerge=startmerge->next;
                        }
        }while(start1&&start2);
    }
    

    【讨论】:

    • 嗯……对标有 C 的问题使用 C++ 解决方案并不是世界上最好的选择。
    猜你喜欢
    • 2021-04-24
    • 2017-02-24
    • 1970-01-01
    • 2013-08-12
    • 2020-05-14
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多