I am creating a polynomial using a linked list in C, and I'm having a problem. Can someone please help me with my code? In the create
function, I have just created a node, and I want to place the node in the right position in the function insert
, and I then want the polynomial p1
to be returned.
我用C中的链表创建一个多项式,我有一个问题。有人能帮我写代码吗?在create函数中,我刚刚创建了一个节点,我想把节点放在函数插入的正确位置,然后我想要返回多项式p1。
I am also not able to understand how the return
statement is going to work. Please tell me what the errors in the code are with my approach.
我也无法理解返回语句是如何工作的。请告诉我代码中有哪些错误。
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;
}
2 个解决方案
#1
1
This code works for at least some inputs:
该代码至少适用于一些输入:
#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);
}
Fixes include:
修复程序包括:
- Testing for I/O errors
- 检测I / O错误
- Add tag to display function
- 添加标签以显示功能。
- Use display function copiously
- 使用显示功能丰富
- Add error exit function and use it
- 添加错误退出函数并使用它。
-
Primary fix: handle test in
while
loop ininsert()
correctly:- Test
temp->link
for nullness - 试验温度为nullness - >链接
- Use
&&
and not||
in testing for validity - 使用&&而不是||测试有效性。
- Test
- 主修复:在insert()的while循环中进行句柄测试:测试temp->链接为nullness使用&&而不是||在测试中的有效性。
- Improve printing in
display()
(only output+
when it separates two terms; output newline at end) - 在显示()中改进打印(只有输出+分隔两项时);输出换行符结束)
- Don't leak memory in
main()
. - 不要在main()中泄漏内存。
- Don't pass uninitialized memory to
create()
. - 不要传递未初始化的内存来创建()。
- Don't ignore return from
create()
. - 不要忽略create()的返回。
Example run:
示例运行:
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
There is room to think the ordering by exponent is not working correctly, but the code doesn't crash. Running with valgrind
spots no memory access errors; it leaks like the proverbial sieve, though.
有空间认为指数的排序不能正常工作,但是代码不会崩溃。运行时,没有内存访问错误;不过,它就像众所周知的筛子一样漏了。
#2
0
I hope it helps you out.You need to reduce some of the redundancies though.
我希望它能帮到你。不过,您需要减少一些冗余。
#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);
}
#1
1
This code works for at least some inputs:
该代码至少适用于一些输入:
#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);
}
Fixes include:
修复程序包括:
- Testing for I/O errors
- 检测I / O错误
- Add tag to display function
- 添加标签以显示功能。
- Use display function copiously
- 使用显示功能丰富
- Add error exit function and use it
- 添加错误退出函数并使用它。
-
Primary fix: handle test in
while
loop ininsert()
correctly:- Test
temp->link
for nullness - 试验温度为nullness - >链接
- Use
&&
and not||
in testing for validity - 使用&&而不是||测试有效性。
- Test
- 主修复:在insert()的while循环中进行句柄测试:测试temp->链接为nullness使用&&而不是||在测试中的有效性。
- Improve printing in
display()
(only output+
when it separates two terms; output newline at end) - 在显示()中改进打印(只有输出+分隔两项时);输出换行符结束)
- Don't leak memory in
main()
. - 不要在main()中泄漏内存。
- Don't pass uninitialized memory to
create()
. - 不要传递未初始化的内存来创建()。
- Don't ignore return from
create()
. - 不要忽略create()的返回。
Example run:
示例运行:
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
There is room to think the ordering by exponent is not working correctly, but the code doesn't crash. Running with valgrind
spots no memory access errors; it leaks like the proverbial sieve, though.
有空间认为指数的排序不能正常工作,但是代码不会崩溃。运行时,没有内存访问错误;不过,它就像众所周知的筛子一样漏了。
#2
0
I hope it helps you out.You need to reduce some of the redundancies though.
我希望它能帮到你。不过,您需要减少一些冗余。
#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);
}